import type { MilkdownPlugin, TimerType } from '@milkdown/ctx' import type { MarkdownSource, Parser } from '@milkdown/transformer' import { createSlice, createTimer } from '@milkdown/ctx' import { ctxCallOutOfScope } from '@milkdown/exception' import { ParserState } from '@milkdown/transformer' import { ParsedSources, parsedSourcesCtx, withMeta } from '../__internal__' import { remarkCtx } from './atoms' import { preserveSourceCtx, sourceParserCtx } from './markdown-source' import { SchemaReady, schemaCtx } from './schema' /// The timer which will be resolved when the parser plugin is ready. export const ParserReady = createTimer('ParserReady') const outOfScope = (() => { throw ctxCallOutOfScope() }) as Parser /// A slice which contains the parser. export const parserCtx = createSlice(outOfScope, 'parser') /// A slice that holds the timers to wait for before the plugin /// runs. It defaults to `[SchemaReady]`. export const parserTimerCtx = createSlice([] as TimerType[], 'parserTimer') /// The parser plugin. /// This plugin will create a parser. /// /// This plugin waits for the schema plugin. export const parser: MilkdownPlugin = (ctx) => { ctx .inject(parserCtx, outOfScope) .inject(preserveSourceCtx, false) // A new instance for each editor. .inject(parsedSourcesCtx, new ParsedSources()) .inject(sourceParserCtx) .inject(parserTimerCtx, [SchemaReady]) .record(ParserReady) return async () => { await ctx.waitTimers(parserTimerCtx) const remark = ctx.get(remarkCtx) const schema = ctx.get(schemaCtx) const preserveSource = ctx.get(preserveSourceCtx) const sources = ctx.get(parsedSourcesCtx) // The parser keeps nothing. It also parses what never becomes the // document: a paste, an inserted snippet, a chunk of a stream. const parse = ParserState.create(schema, remark) ctx.set(parserCtx, parse) // The source parser is for markdown that is loaded as the document. // With source preservation on, it remembers the text of what it // parses, and where each block is in that text. const state = new ParserState(schema) ctx.set(sourceParserCtx, (markdown) => { // Read the slice on every call, so a parser that a plugin replaced // or wrapped is the one that runs. const parser = ctx.get(parserCtx) // Code without types can pass something that is not a string, and // remark reads it as a file. That is no markdown to keep. if (!preserveSource || typeof markdown !== 'string') return { doc: parser(markdown), source: null } // Only the parser of the editor can tell where the blocks are. A // parser from a plugin gives the document and nothing more. let source: MarkdownSource if (parser === parse) { const { doc, blocks, detached } = state.run(remark, markdown).toResult() source = { source: markdown, doc, blocks, detached } } else { source = { source: markdown, doc: parser(markdown), blocks: null, detached: [], } } sources.record(source) return { doc: source.doc, source } }) ctx.done(ParserReady) return () => { ctx .remove(parserCtx) .remove(preserveSourceCtx) .remove(parsedSourcesCtx) .remove(sourceParserCtx) .remove(parserTimerCtx) .clearTimer(ParserReady) } } } withMeta(parser, { displayName: 'Parser', })