123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- /**
- * @typedef {import('micromark-util-types').Code} Code
- * @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 {factorySpace} from 'micromark-factory-space'
- import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
- import {codes, constants, types} from 'micromark-util-symbol'
- import {ok as assert} from 'devlop'
- /** @type {Construct} */
- const nonLazyContinuation = {
- tokenize: tokenizeNonLazyContinuation,
- partial: true
- }
- /** @type {Construct} */
- export const codeFenced = {
- name: 'codeFenced',
- tokenize: tokenizeCodeFenced,
- concrete: true
- }
- /**
- * @this {TokenizeContext}
- * @type {Tokenizer}
- */
- function tokenizeCodeFenced(effects, ok, nok) {
- const self = this
- /** @type {Construct} */
- const closeStart = {tokenize: tokenizeCloseStart, partial: true}
- let initialPrefix = 0
- let sizeOpen = 0
- /** @type {NonNullable<Code>} */
- let marker
- return start
- /**
- * Start of code.
- *
- * ```markdown
- * > | ~~~js
- * ^
- * | alert(1)
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function start(code) {
- // To do: parse whitespace like `markdown-rs`.
- return beforeSequenceOpen(code)
- }
- /**
- * In opening fence, after prefix, at sequence.
- *
- * ```markdown
- * > | ~~~js
- * ^
- * | alert(1)
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function beforeSequenceOpen(code) {
- assert(
- code === codes.graveAccent || code === codes.tilde,
- 'expected `` ` `` or `~`'
- )
- const tail = self.events[self.events.length - 1]
- initialPrefix =
- tail && tail[1].type === types.linePrefix
- ? tail[2].sliceSerialize(tail[1], true).length
- : 0
- marker = code
- effects.enter(types.codeFenced)
- effects.enter(types.codeFencedFence)
- effects.enter(types.codeFencedFenceSequence)
- return sequenceOpen(code)
- }
- /**
- * In opening fence sequence.
- *
- * ```markdown
- * > | ~~~js
- * ^
- * | alert(1)
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function sequenceOpen(code) {
- if (code === marker) {
- sizeOpen++
- effects.consume(code)
- return sequenceOpen
- }
- if (sizeOpen < constants.codeFencedSequenceSizeMin) {
- return nok(code)
- }
- effects.exit(types.codeFencedFenceSequence)
- return markdownSpace(code)
- ? factorySpace(effects, infoBefore, types.whitespace)(code)
- : infoBefore(code)
- }
- /**
- * In opening fence, after the sequence (and optional whitespace), before info.
- *
- * ```markdown
- * > | ~~~js
- * ^
- * | alert(1)
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function infoBefore(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- effects.exit(types.codeFencedFence)
- return self.interrupt
- ? ok(code)
- : effects.check(nonLazyContinuation, atNonLazyBreak, after)(code)
- }
- effects.enter(types.codeFencedFenceInfo)
- effects.enter(types.chunkString, {contentType: constants.contentTypeString})
- return info(code)
- }
- /**
- * In info.
- *
- * ```markdown
- * > | ~~~js
- * ^
- * | alert(1)
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function info(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- effects.exit(types.chunkString)
- effects.exit(types.codeFencedFenceInfo)
- return infoBefore(code)
- }
- if (markdownSpace(code)) {
- effects.exit(types.chunkString)
- effects.exit(types.codeFencedFenceInfo)
- return factorySpace(effects, metaBefore, types.whitespace)(code)
- }
- if (code === codes.graveAccent && code === marker) {
- return nok(code)
- }
- effects.consume(code)
- return info
- }
- /**
- * In opening fence, after info and whitespace, before meta.
- *
- * ```markdown
- * > | ~~~js eval
- * ^
- * | alert(1)
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function metaBefore(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- return infoBefore(code)
- }
- effects.enter(types.codeFencedFenceMeta)
- effects.enter(types.chunkString, {contentType: constants.contentTypeString})
- return meta(code)
- }
- /**
- * In meta.
- *
- * ```markdown
- * > | ~~~js eval
- * ^
- * | alert(1)
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function meta(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- effects.exit(types.chunkString)
- effects.exit(types.codeFencedFenceMeta)
- return infoBefore(code)
- }
- if (code === codes.graveAccent && code === marker) {
- return nok(code)
- }
- effects.consume(code)
- return meta
- }
- /**
- * At eol/eof in code, before a non-lazy closing fence or content.
- *
- * ```markdown
- * > | ~~~js
- * ^
- * > | alert(1)
- * ^
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function atNonLazyBreak(code) {
- assert(markdownLineEnding(code), 'expected eol')
- return effects.attempt(closeStart, after, contentBefore)(code)
- }
- /**
- * Before code content, not a closing fence, at eol.
- *
- * ```markdown
- * | ~~~js
- * > | alert(1)
- * ^
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function contentBefore(code) {
- assert(markdownLineEnding(code), 'expected eol')
- effects.enter(types.lineEnding)
- effects.consume(code)
- effects.exit(types.lineEnding)
- return contentStart
- }
- /**
- * Before code content, not a closing fence.
- *
- * ```markdown
- * | ~~~js
- * > | alert(1)
- * ^
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function contentStart(code) {
- return initialPrefix > 0 && markdownSpace(code)
- ? factorySpace(
- effects,
- beforeContentChunk,
- types.linePrefix,
- initialPrefix + 1
- )(code)
- : beforeContentChunk(code)
- }
- /**
- * Before code content, after optional prefix.
- *
- * ```markdown
- * | ~~~js
- * > | alert(1)
- * ^
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function beforeContentChunk(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- return effects.check(nonLazyContinuation, atNonLazyBreak, after)(code)
- }
- effects.enter(types.codeFlowValue)
- return contentChunk(code)
- }
- /**
- * In code content.
- *
- * ```markdown
- * | ~~~js
- * > | alert(1)
- * ^^^^^^^^
- * | ~~~
- * ```
- *
- * @type {State}
- */
- function contentChunk(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- effects.exit(types.codeFlowValue)
- return beforeContentChunk(code)
- }
- effects.consume(code)
- return contentChunk
- }
- /**
- * After code.
- *
- * ```markdown
- * | ~~~js
- * | alert(1)
- * > | ~~~
- * ^
- * ```
- *
- * @type {State}
- */
- function after(code) {
- effects.exit(types.codeFenced)
- return ok(code)
- }
- /**
- * @this {TokenizeContext}
- * @type {Tokenizer}
- */
- function tokenizeCloseStart(effects, ok, nok) {
- let size = 0
- return startBefore
- /**
- *
- *
- * @type {State}
- */
- function startBefore(code) {
- assert(markdownLineEnding(code), 'expected eol')
- effects.enter(types.lineEnding)
- effects.consume(code)
- effects.exit(types.lineEnding)
- return start
- }
- /**
- * Before closing fence, at optional whitespace.
- *
- * ```markdown
- * | ~~~js
- * | alert(1)
- * > | ~~~
- * ^
- * ```
- *
- * @type {State}
- */
- function start(code) {
- // Always populated by defaults.
- assert(
- self.parser.constructs.disable.null,
- 'expected `disable.null` to be populated'
- )
- // To do: `enter` here or in next state?
- effects.enter(types.codeFencedFence)
- return markdownSpace(code)
- ? factorySpace(
- effects,
- beforeSequenceClose,
- types.linePrefix,
- self.parser.constructs.disable.null.includes('codeIndented')
- ? undefined
- : constants.tabSize
- )(code)
- : beforeSequenceClose(code)
- }
- /**
- * In closing fence, after optional whitespace, at sequence.
- *
- * ```markdown
- * | ~~~js
- * | alert(1)
- * > | ~~~
- * ^
- * ```
- *
- * @type {State}
- */
- function beforeSequenceClose(code) {
- if (code === marker) {
- effects.enter(types.codeFencedFenceSequence)
- return sequenceClose(code)
- }
- return nok(code)
- }
- /**
- * In closing fence sequence.
- *
- * ```markdown
- * | ~~~js
- * | alert(1)
- * > | ~~~
- * ^
- * ```
- *
- * @type {State}
- */
- function sequenceClose(code) {
- if (code === marker) {
- size++
- effects.consume(code)
- return sequenceClose
- }
- if (size >= sizeOpen) {
- effects.exit(types.codeFencedFenceSequence)
- return markdownSpace(code)
- ? factorySpace(effects, sequenceCloseAfter, types.whitespace)(code)
- : sequenceCloseAfter(code)
- }
- return nok(code)
- }
- /**
- * After closing fence sequence, after optional whitespace.
- *
- * ```markdown
- * | ~~~js
- * | alert(1)
- * > | ~~~
- * ^
- * ```
- *
- * @type {State}
- */
- function sequenceCloseAfter(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- effects.exit(types.codeFencedFence)
- return ok(code)
- }
- return nok(code)
- }
- }
- }
- /**
- * @this {TokenizeContext}
- * @type {Tokenizer}
- */
- function tokenizeNonLazyContinuation(effects, ok, nok) {
- const self = this
- return start
- /**
- *
- *
- * @type {State}
- */
- function start(code) {
- if (code === codes.eof) {
- return nok(code)
- }
- assert(markdownLineEnding(code), 'expected eol')
- effects.enter(types.lineEnding)
- effects.consume(code)
- effects.exit(types.lineEnding)
- return lineStart
- }
- /**
- *
- *
- * @type {State}
- */
- function lineStart(code) {
- return self.parser.lazy[self.now().line] ? nok(code) : ok(code)
- }
- }
|