| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /**
- * @import {
- * Construct,
- * State,
- * TokenizeContext,
- * Tokenizer
- * } from 'micromark-util-types'
- */
- import { asciiAlphanumeric, asciiAlpha, asciiAtext, asciiControl } from 'micromark-util-character';
- /** @type {Construct} */
- export const autolink = {
- name: 'autolink',
- tokenize: tokenizeAutolink
- };
- /**
- * @this {TokenizeContext}
- * Context.
- * @type {Tokenizer}
- */
- function tokenizeAutolink(effects, ok, nok) {
- let size = 0;
- return start;
- /**
- * Start of an autolink.
- *
- * ```markdown
- * > | a<https://example.com>b
- * ^
- * > | a<user@example.com>b
- * ^
- * ```
- *
- * @type {State}
- */
- function start(code) {
- effects.enter("autolink");
- effects.enter("autolinkMarker");
- effects.consume(code);
- effects.exit("autolinkMarker");
- effects.enter("autolinkProtocol");
- return open;
- }
- /**
- * After `<`, at protocol or atext.
- *
- * ```markdown
- * > | a<https://example.com>b
- * ^
- * > | a<user@example.com>b
- * ^
- * ```
- *
- * @type {State}
- */
- function open(code) {
- if (asciiAlpha(code)) {
- effects.consume(code);
- return schemeOrEmailAtext;
- }
- if (code === 64) {
- return nok(code);
- }
- return emailAtext(code);
- }
- /**
- * At second byte of protocol or atext.
- *
- * ```markdown
- * > | a<https://example.com>b
- * ^
- * > | a<user@example.com>b
- * ^
- * ```
- *
- * @type {State}
- */
- function schemeOrEmailAtext(code) {
- // ASCII alphanumeric and `+`, `-`, and `.`.
- if (code === 43 || code === 45 || code === 46 || asciiAlphanumeric(code)) {
- // Count the previous alphabetical from `open` too.
- size = 1;
- return schemeInsideOrEmailAtext(code);
- }
- return emailAtext(code);
- }
- /**
- * In ambiguous protocol or atext.
- *
- * ```markdown
- * > | a<https://example.com>b
- * ^
- * > | a<user@example.com>b
- * ^
- * ```
- *
- * @type {State}
- */
- function schemeInsideOrEmailAtext(code) {
- if (code === 58) {
- effects.consume(code);
- size = 0;
- return urlInside;
- }
- // ASCII alphanumeric and `+`, `-`, and `.`.
- if ((code === 43 || code === 45 || code === 46 || asciiAlphanumeric(code)) && size++ < 32) {
- effects.consume(code);
- return schemeInsideOrEmailAtext;
- }
- size = 0;
- return emailAtext(code);
- }
- /**
- * After protocol, in URL.
- *
- * ```markdown
- * > | a<https://example.com>b
- * ^
- * ```
- *
- * @type {State}
- */
- function urlInside(code) {
- if (code === 62) {
- effects.exit("autolinkProtocol");
- effects.enter("autolinkMarker");
- effects.consume(code);
- effects.exit("autolinkMarker");
- effects.exit("autolink");
- return ok;
- }
- // ASCII control, space, or `<`.
- if (code === null || code === 32 || code === 60 || asciiControl(code)) {
- return nok(code);
- }
- effects.consume(code);
- return urlInside;
- }
- /**
- * In email atext.
- *
- * ```markdown
- * > | a<user.name@example.com>b
- * ^
- * ```
- *
- * @type {State}
- */
- function emailAtext(code) {
- if (code === 64) {
- effects.consume(code);
- return emailAtSignOrDot;
- }
- if (asciiAtext(code)) {
- effects.consume(code);
- return emailAtext;
- }
- return nok(code);
- }
- /**
- * In label, after at-sign or dot.
- *
- * ```markdown
- * > | a<user.name@example.com>b
- * ^ ^
- * ```
- *
- * @type {State}
- */
- function emailAtSignOrDot(code) {
- return asciiAlphanumeric(code) ? emailLabel(code) : nok(code);
- }
- /**
- * In label, where `.` and `>` are allowed.
- *
- * ```markdown
- * > | a<user.name@example.com>b
- * ^
- * ```
- *
- * @type {State}
- */
- function emailLabel(code) {
- if (code === 46) {
- effects.consume(code);
- size = 0;
- return emailAtSignOrDot;
- }
- if (code === 62) {
- // Exit, then change the token type.
- effects.exit("autolinkProtocol").type = "autolinkEmail";
- effects.enter("autolinkMarker");
- effects.consume(code);
- effects.exit("autolinkMarker");
- effects.exit("autolink");
- return ok;
- }
- return emailValue(code);
- }
- /**
- * In label, where `.` and `>` are *not* allowed.
- *
- * Though, this is also used in `emailLabel` to parse other values.
- *
- * ```markdown
- * > | a<user.name@ex-ample.com>b
- * ^
- * ```
- *
- * @type {State}
- */
- function emailValue(code) {
- // ASCII alphanumeric or `-`.
- if ((code === 45 || asciiAlphanumeric(code)) && size++ < 63) {
- const next = code === 45 ? emailValue : emailLabel;
- effects.consume(code);
- return next;
- }
- return nok(code);
- }
- }
|