123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- /**
- * @typedef {import('micromark-util-types').Construct} Construct
- * @typedef {import('micromark-util-types').State} State
- * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
- * @typedef {import('micromark-util-types').Tokenizer} Tokenizer
- */
- import {factoryDestination} from 'micromark-factory-destination'
- import {factoryLabel} from 'micromark-factory-label'
- import {factorySpace} from 'micromark-factory-space'
- import {factoryTitle} from 'micromark-factory-title'
- import {factoryWhitespace} from 'micromark-factory-whitespace'
- import {
- markdownLineEnding,
- markdownLineEndingOrSpace,
- markdownSpace
- } from 'micromark-util-character'
- import {normalizeIdentifier} from 'micromark-util-normalize-identifier'
- import {codes, types} from 'micromark-util-symbol'
- import {ok as assert} from 'devlop'
- /** @type {Construct} */
- export const definition = {name: 'definition', tokenize: tokenizeDefinition}
- /** @type {Construct} */
- const titleBefore = {tokenize: tokenizeTitleBefore, partial: true}
- /**
- * @this {TokenizeContext}
- * @type {Tokenizer}
- */
- function tokenizeDefinition(effects, ok, nok) {
- const self = this
- /** @type {string} */
- let identifier
- return start
- /**
- * At start of a definition.
- *
- * ```markdown
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function start(code) {
- // Do not interrupt paragraphs (but do follow definitions).
- // To do: do `interrupt` the way `markdown-rs` does.
- // To do: parse whitespace the way `markdown-rs` does.
- effects.enter(types.definition)
- return before(code)
- }
- /**
- * After optional whitespace, at `[`.
- *
- * ```markdown
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function before(code) {
- // To do: parse whitespace the way `markdown-rs` does.
- assert(code === codes.leftSquareBracket, 'expected `[`')
- return factoryLabel.call(
- self,
- effects,
- labelAfter,
- // Note: we don’t need to reset the way `markdown-rs` does.
- nok,
- types.definitionLabel,
- types.definitionLabelMarker,
- types.definitionLabelString
- )(code)
- }
- /**
- * After label.
- *
- * ```markdown
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function labelAfter(code) {
- identifier = normalizeIdentifier(
- self.sliceSerialize(self.events[self.events.length - 1][1]).slice(1, -1)
- )
- if (code === codes.colon) {
- effects.enter(types.definitionMarker)
- effects.consume(code)
- effects.exit(types.definitionMarker)
- return markerAfter
- }
- return nok(code)
- }
- /**
- * After marker.
- *
- * ```markdown
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function markerAfter(code) {
- // Note: whitespace is optional.
- return markdownLineEndingOrSpace(code)
- ? factoryWhitespace(effects, destinationBefore)(code)
- : destinationBefore(code)
- }
- /**
- * Before destination.
- *
- * ```markdown
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function destinationBefore(code) {
- return factoryDestination(
- effects,
- destinationAfter,
- // Note: we don’t need to reset the way `markdown-rs` does.
- nok,
- types.definitionDestination,
- types.definitionDestinationLiteral,
- types.definitionDestinationLiteralMarker,
- types.definitionDestinationRaw,
- types.definitionDestinationString
- )(code)
- }
- /**
- * After destination.
- *
- * ```markdown
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function destinationAfter(code) {
- return effects.attempt(titleBefore, after, after)(code)
- }
- /**
- * After definition.
- *
- * ```markdown
- * > | [a]: b
- * ^
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function after(code) {
- return markdownSpace(code)
- ? factorySpace(effects, afterWhitespace, types.whitespace)(code)
- : afterWhitespace(code)
- }
- /**
- * After definition, after optional whitespace.
- *
- * ```markdown
- * > | [a]: b
- * ^
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function afterWhitespace(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- effects.exit(types.definition)
- // Note: we don’t care about uniqueness.
- // It’s likely that that doesn’t happen very frequently.
- // It is more likely that it wastes precious time.
- self.parser.defined.push(identifier)
- // To do: `markdown-rs` interrupt.
- // // You’d be interrupting.
- // tokenizer.interrupt = true
- return ok(code)
- }
- return nok(code)
- }
- }
- /**
- * @this {TokenizeContext}
- * @type {Tokenizer}
- */
- function tokenizeTitleBefore(effects, ok, nok) {
- return titleBefore
- /**
- * After destination, at whitespace.
- *
- * ```markdown
- * > | [a]: b
- * ^
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function titleBefore(code) {
- return markdownLineEndingOrSpace(code)
- ? factoryWhitespace(effects, beforeMarker)(code)
- : nok(code)
- }
- /**
- * At title.
- *
- * ```markdown
- * | [a]: b
- * > | "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function beforeMarker(code) {
- return factoryTitle(
- effects,
- titleAfter,
- nok,
- types.definitionTitle,
- types.definitionTitleMarker,
- types.definitionTitleString
- )(code)
- }
- /**
- * After title.
- *
- * ```markdown
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function titleAfter(code) {
- return markdownSpace(code)
- ? factorySpace(
- effects,
- titleAfterOptionalWhitespace,
- types.whitespace
- )(code)
- : titleAfterOptionalWhitespace(code)
- }
- /**
- * After title, after optional whitespace.
- *
- * ```markdown
- * > | [a]: b "c"
- * ^
- * ```
- *
- * @type {State}
- */
- function titleAfterOptionalWhitespace(code) {
- return code === codes.eof || markdownLineEnding(code) ? ok(code) : nok(code)
- }
- }
|