@yozora/tokenizer-autolink
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
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])?)*$/
- See github flavor markdown spec for details.
- See Live Examples for an intuitive impression.
Install
- npm
- Yarn
- pnpm
npm install --save @yozora/tokenizer-autolink
yarn add @yozora/tokenizer-autolink
pnpm add @yozora/tokenizer-autolink
Usage
@yozora/tokenizer-autolink has been integrated into @yozora/parser / @yozora/parser-gfm-ex / @yozora/parser-gfm,
so you can use YozoraParser
/ GfmExParser
/ GfmParser
directly.
- Basic Usage
- YozoraParser
- GfmParser
- GfmExParser
@yozora/tokenizer-autolink cannot be used alone, it needs to be registered in YastParser as a plugin-in before it can be used.
import { DefaultYastParser } from '@yozora/core-parser'
import ParagraphTokenizer from '@yozora/tokenizer-paragraph'
import TextTokenizer from '@yozora/tokenizer-text'
import AutolinkTokenizer from '@yozora/tokenizer-autolink'
const parser = new DefaultYastParser()
.useBlockFallbackTokenizer(new ParagraphTokenizer())
.useInlineFallbackTokenizer(new TextTokenizer())
.useTokenizer(new AutolinkTokenizer())
// parse source markdown content
parser.parse(`
<foo@bar.example.com>
<http://foo.bar.baz>
<made-up-scheme://foo,bar>
`)
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>
`)
import GfmParser from '@yozora/parser-gfm'
const parser = new GfmParser()
// parse source markdown content
parser.parse(`
<foo@bar.example.com>
<http://foo.bar.baz>
<made-up-scheme://foo,bar>
`)
import GfmExParser from '@yozora/parser-gfm-ex'
const parser = new GfmExParser()
// parse source markdown content
parser.parse(`
<foo@bar.example.com>
<http://foo.bar.baz>
<made-up-scheme://foo,bar>
`)
Options
Name | Type | Required | Default |
---|---|---|---|
name | string | false | "@yozora/tokenizer-autolink" |
priority | number | false | TokenizerPriority.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 thematch-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 { YastParent, YastResource } 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 YastParent<LinkType>, YastResource {}
Live Examples
-
Not autolinks.