Rich text / foldkit-richtext-markdown
Parse Markdown
Parse Markdown into a validated document by providing an ID generator (mint):
import { parse } from 'foldkit-richtext-markdown'
let n = 0
const { document: parsedDoc, diagnostics: parseDiagnostics } = parse('> quoted\n', {
mint: () => `id-${++n}`,
})mint supplies every node and run ID, and the result runs through the document codec so invalid structure fails immediately.
parse reads CommonMark plus GFM task lists, strikethrough, and tables. Pass footnotes: true to decode footnotes into FootnoteRef and FootnoteDefinition nodes; otherwise footnotes are skipped and reported. Rather than guessing, parse reports raw HTML, link reference definitions (keeping reference text unlinked), hard line breaks (split into separate paragraphs), and images inside paragraphs (hoisted when alone). Every link href and image src passes the same safeUrl check used by HTML import; unsafe URLs keep link text unlinked, drop images, and report UnsafeUrl.
Canonical printing and parsing round-trip cleanly: print(parse(markdown)) reproduces the canonical Markdown (* emphasis, - bullets, backtick fences, # headings), and parse(print(document)) reproduces an equivalent document.
To preserve a writer's original formatting choices (such as _hello_, * item, or === underlined headings), pass the style object returned by parse back into print:
const parsed = parse('_hello_\n\n* item\n', { mint: () => `id-${++n}` })
print(parsed.document, { style: parsed.style }).markdown // '_hello_\n\n* item\n'style.blocks records how each parsed list, heading, fence, and thematic break was written, including loose vs. tight list spacing (defaulting to tight without a style). Blocks added later in the rich editor fall back to the construct's default spelling. If applying a saved style would change the parsed meaning (for example, _ inside a word), print falls back to canonical syntax.