AK-21/Graphite-Industrial-Intelligence
0
1/**2 * @import {3 * Construct,4 * Resolver,5 * State,6 * TokenizeContext,7 * Tokenizer,8 * Token9 * } from 'micromark-util-types'10 */11 12import { factorySpace } from 'micromark-factory-space';13import { markdownLineEnding } from 'micromark-util-character';14import { subtokenize } from 'micromark-util-subtokenize';15/**16 * No name because it must not be turned off.17 * @type {Construct}18 */19export const content = {20 resolve: resolveContent,21 tokenize: tokenizeContent22};23 24/** @type {Construct} */25const continuationConstruct = {26 partial: true,27 tokenize: tokenizeContinuation28};29 30/**31 * Content is transparent: it’s parsed right now. That way, definitions are also32 * parsed right now: before text in paragraphs (specifically, media) are parsed.33 *34 * @type {Resolver}35 */36function resolveContent(events) {37 subtokenize(events);38 return events;39}40 41/**42 * @this {TokenizeContext}43 * Context.44 * @type {Tokenizer}45 */46function tokenizeContent(effects, ok) {47 /** @type {Token | undefined} */48 let previous;49 return chunkStart;50 51 /**52 * Before a content chunk.53 *54 * ```markdown55 * > | abc56 * ^57 * ```58 *59 * @type {State}60 */61 function chunkStart(code) {62 effects.enter("content");63 previous = effects.enter("chunkContent", {64 contentType: "content"65 });66 return chunkInside(code);67 }68 69 /**70 * In a content chunk.71 *72 * ```markdown73 * > | abc74 * ^^^75 * ```76 *77 * @type {State}78 */79 function chunkInside(code) {80 if (code === null) {81 return contentEnd(code);82 }83 84 // To do: in `markdown-rs`, each line is parsed on its own, and everything85 // is stitched together resolving.86 if (markdownLineEnding(code)) {87 return effects.check(continuationConstruct, contentContinue, contentEnd)(code);88 }89 90 // Data.91 effects.consume(code);92 return chunkInside;93 }94 95 /**96 *97 *98 * @type {State}99 */100 function contentEnd(code) {101 effects.exit("chunkContent");102 effects.exit("content");103 return ok(code);104 }105 106 /**107 *108 *109 * @type {State}110 */111 function contentContinue(code) {112 effects.consume(code);113 effects.exit("chunkContent");114 previous.next = effects.enter("chunkContent", {115 contentType: "content",116 previous117 });118 previous = previous.next;119 return chunkInside;120 }121}122 123/**124 * @this {TokenizeContext}125 * Context.126 * @type {Tokenizer}127 */128function tokenizeContinuation(effects, ok, nok) {129 const self = this;130 return startLookahead;131 132 /**133 *134 *135 * @type {State}136 */137 function startLookahead(code) {138 effects.exit("chunkContent");139 effects.enter("lineEnding");140 effects.consume(code);141 effects.exit("lineEnding");142 return factorySpace(effects, prefixed, "linePrefix");143 }144 145 /**146 *147 *148 * @type {State}149 */150 function prefixed(code) {151 if (code === null || markdownLineEnding(code)) {152 return nok(code);153 }154 155 // Always populated by defaults.156 157 const tail = self.events[self.events.length - 1];158 if (!self.parser.constructs.disable.null.includes('codeIndented') && tail && tail[1].type === "linePrefix" && tail[2].sliceSerialize(tail[1], true).length >= 4) {159 return ok(code);160 }161 return effects.interrupt(self.parser.constructs.flow, nok, ok)(code);162 }163}