| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- /**
- * @import {Construct, State, TokenizeContext, Tokenizer} from 'micromark-util-types'
- */
- import { factorySpace } from 'micromark-factory-space';
- import { markdownLineEnding } from 'micromark-util-character';
- /** @type {Construct} */
- export const mathFlow = {
- tokenize: tokenizeMathFenced,
- concrete: true,
- name: 'mathFlow'
- };
- /** @type {Construct} */
- const nonLazyContinuation = {
- tokenize: tokenizeNonLazyContinuation,
- partial: true
- };
- /**
- * @this {TokenizeContext}
- * @type {Tokenizer}
- */
- function tokenizeMathFenced(effects, ok, nok) {
- const self = this;
- const tail = self.events[self.events.length - 1];
- const initialSize = tail && tail[1].type === "linePrefix" ? tail[2].sliceSerialize(tail[1], true).length : 0;
- let sizeOpen = 0;
- return start;
- /**
- * Start of math.
- *
- * ```markdown
- * > | $$
- * ^
- * | \frac{1}{2}
- * | $$
- * ```
- *
- * @type {State}
- */
- function start(code) {
- effects.enter('mathFlow');
- effects.enter('mathFlowFence');
- effects.enter('mathFlowFenceSequence');
- return sequenceOpen(code);
- }
- /**
- * In opening fence sequence.
- *
- * ```markdown
- * > | $$
- * ^
- * | \frac{1}{2}
- * | $$
- * ```
- *
- * @type {State}
- */
- function sequenceOpen(code) {
- if (code === 36) {
- effects.consume(code);
- sizeOpen++;
- return sequenceOpen;
- }
- if (sizeOpen < 2) {
- return nok(code);
- }
- effects.exit('mathFlowFenceSequence');
- return factorySpace(effects, metaBefore, "whitespace")(code);
- }
- /**
- * In opening fence, before meta.
- *
- * ```markdown
- * > | $$asciimath
- * ^
- * | x < y
- * | $$
- * ```
- *
- * @type {State}
- */
- function metaBefore(code) {
- if (code === null || markdownLineEnding(code)) {
- return metaAfter(code);
- }
- effects.enter('mathFlowFenceMeta');
- effects.enter("chunkString", {
- contentType: "string"
- });
- return meta(code);
- }
- /**
- * In meta.
- *
- * ```markdown
- * > | $$asciimath
- * ^
- * | x < y
- * | $$
- * ```
- *
- * @type {State}
- */
- function meta(code) {
- if (code === null || markdownLineEnding(code)) {
- effects.exit("chunkString");
- effects.exit('mathFlowFenceMeta');
- return metaAfter(code);
- }
- if (code === 36) {
- return nok(code);
- }
- effects.consume(code);
- return meta;
- }
- /**
- * After meta.
- *
- * ```markdown
- * > | $$
- * ^
- * | \frac{1}{2}
- * | $$
- * ```
- *
- * @type {State}
- */
- function metaAfter(code) {
- // Guaranteed to be eol/eof.
- effects.exit('mathFlowFence');
- if (self.interrupt) {
- return ok(code);
- }
- return effects.attempt(nonLazyContinuation, beforeNonLazyContinuation, after)(code);
- }
- /**
- * After eol/eof in math, at a non-lazy closing fence or content.
- *
- * ```markdown
- * | $$
- * > | \frac{1}{2}
- * ^
- * > | $$
- * ^
- * ```
- *
- * @type {State}
- */
- function beforeNonLazyContinuation(code) {
- return effects.attempt({
- tokenize: tokenizeClosingFence,
- partial: true
- }, after, contentStart)(code);
- }
- /**
- * Before math content, definitely not before a closing fence.
- *
- * ```markdown
- * | $$
- * > | \frac{1}{2}
- * ^
- * | $$
- * ```
- *
- * @type {State}
- */
- function contentStart(code) {
- return (initialSize ? factorySpace(effects, beforeContentChunk, "linePrefix", initialSize + 1) : beforeContentChunk)(code);
- }
- /**
- * Before math content, after optional prefix.
- *
- * ```markdown
- * | $$
- * > | \frac{1}{2}
- * ^
- * | $$
- * ```
- *
- * @type {State}
- */
- function beforeContentChunk(code) {
- if (code === null) {
- return after(code);
- }
- if (markdownLineEnding(code)) {
- return effects.attempt(nonLazyContinuation, beforeNonLazyContinuation, after)(code);
- }
- effects.enter('mathFlowValue');
- return contentChunk(code);
- }
- /**
- * In math content.
- *
- * ```markdown
- * | $$
- * > | \frac{1}{2}
- * ^
- * | $$
- * ```
- *
- * @type {State}
- */
- function contentChunk(code) {
- if (code === null || markdownLineEnding(code)) {
- effects.exit('mathFlowValue');
- return beforeContentChunk(code);
- }
- effects.consume(code);
- return contentChunk;
- }
- /**
- * After math (ha!).
- *
- * ```markdown
- * | $$
- * | \frac{1}{2}
- * > | $$
- * ^
- * ```
- *
- * @type {State}
- */
- function after(code) {
- effects.exit('mathFlow');
- return ok(code);
- }
- /** @type {Tokenizer} */
- function tokenizeClosingFence(effects, ok, nok) {
- let size = 0;
- /**
- * Before closing fence, at optional whitespace.
- *
- * ```markdown
- * | $$
- * | \frac{1}{2}
- * > | $$
- * ^
- * ```
- */
- return factorySpace(effects, beforeSequenceClose, "linePrefix", self.parser.constructs.disable.null.includes('codeIndented') ? undefined : 4);
- /**
- * In closing fence, after optional whitespace, at sequence.
- *
- * ```markdown
- * | $$
- * | \frac{1}{2}
- * > | $$
- * ^
- * ```
- *
- * @type {State}
- */
- function beforeSequenceClose(code) {
- effects.enter('mathFlowFence');
- effects.enter('mathFlowFenceSequence');
- return sequenceClose(code);
- }
- /**
- * In closing fence sequence.
- *
- * ```markdown
- * | $$
- * | \frac{1}{2}
- * > | $$
- * ^
- * ```
- *
- * @type {State}
- */
- function sequenceClose(code) {
- if (code === 36) {
- size++;
- effects.consume(code);
- return sequenceClose;
- }
- if (size < sizeOpen) {
- return nok(code);
- }
- effects.exit('mathFlowFenceSequence');
- return factorySpace(effects, afterSequenceClose, "whitespace")(code);
- }
- /**
- * After closing fence sequence, after optional whitespace.
- *
- * ```markdown
- * | $$
- * | \frac{1}{2}
- * > | $$
- * ^
- * ```
- *
- * @type {State}
- */
- function afterSequenceClose(code) {
- if (code === null || markdownLineEnding(code)) {
- effects.exit('mathFlowFence');
- 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 === null) {
- return ok(code);
- }
- effects.enter("lineEnding");
- effects.consume(code);
- effects.exit("lineEnding");
- return lineStart;
- }
- /** @type {State} */
- function lineStart(code) {
- return self.parser.lazy[self.now().line] ? nok(code) : ok(code);
- }
- }
|