Skip to main content
Version: 2.x.x

@yozora/tokenizer-autolink

Npm VersionNpm DownloadNpm LicenseModule formats: cjs, esmNode.js VersionTested with JestCode Style: prettier

github flavor markdown spec

Autolinks are absolute URIs and email addresses inside < and >. They are parsed as links, with the URL or email address as the link label.

A URI autolink consists of <, followed by an absolute URI followed by >. It is parsed as a link to the URI, with the URI as the link’s label.

An absolute URI, for these purposes, consists of a scheme followed by a colon (:) followed by zero or more characters other than ASCII whitespace and control characters, <, and >. If the URI includes these characters, they must be percent-encoded (e.g. %20 for a space).

For purposes of this spec, a scheme is any sequence of 2322-32 characters beginning with an ASCII letter and followed by any combination of ASCII letters, digits, or the symbols plus (+), period (.), or hyphen (-).

An email autolink consists of <, followed by an email address, followed by >. The link’s label is the email address, and the URL is mailto: followed by the email address.

An email address, for these purposes, is anything that matches the non-normative regex from the HTML5 spec:

/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

Install

npm install --save @yozora/tokenizer-autolink

Usage

tip

@yozora/tokenizer-autolink has been integrated into @yozora/parser / @yozora/parser-gfm-ex / @yozora/parser-gfm, so you can use YozoraParser / GfmExParser / GfmParser directly.

import YozoraParser from '@yozora/parser'

const parser = new YozoraParser()

// parse source markdown content
parser.parse(`
<foo@bar.example.com>

<http://foo.bar.baz>

<made-up-scheme://foo,bar>
`)

Options

NameTypeRequiredDefault
namestringfalse"@yozora/tokenizer-autolink"
prioritynumberfalseTokenizerPriority.ATOMIC
  • name: The unique name of the tokenizer, used to bind the token it generates, to determine the tokenizer that should be called in each life cycle of the token in the entire matching / parsing phase.

  • priority: Priority of the tokenizer, determine the order of processing, high priority priority execution. interruptable. In addition, in the match-block stage, a high-priority tokenizer can interrupt the matching process of a low-priority tokenizer.

    Exception: Delimiters of type full are always processed before other type delimiters.

Types

@yozora/tokenizer-autolink produce Link type nodes. See @yozora/ast for full base types.

import type { Parent, Resource } from '@yozora/ast'

export const LinkType = 'link'
export type LinkType = typeof LinkType

/**
* Link represents a hyperlink.
* @see https://github.com/syntax-tree/mdast#link
* @see https://github.github.com/gfm/#inline-link
*/
export interface Link extends Parent<LinkType>, Resource {}

Live Examples

  • URI autolink.

      
      
  • Email autolink.

      
      
  • Not autolinks.