MarkdownParser unstable
Parses Markdown source into a Markdown.Document, with no dependencies.
The parser follows CommonMark and adds GFM tables, strikethrough,
autolinks and task lists, and ::Name and :::Name island directives.
Strict parsing reports the construct and line for unsupported syntax:
raw HTML, reference links and images, footnotes, inline
directives, leaf directive labels, and URLs that fail
Markdown.isAllowedUrl. A bare :name in prose, such as 10:30, is
text.
parseMarkdownLenient recovers unsupported syntax as text and returns
diagnostics alongside the document.
It is a pure function of a string, so it runs in a build, a script, a
server or a browser. The Vite plugin in effect/ui/html/MarkdownVite
runs it for every .md import.
Errors
ParseError
unstableMarkdown source that is outside the vocabulary or fails the options'
schemas. line is the 1-based line of the problem, and message names
the construct and the line.
Signature
declare class ParseError extends YieldableError<this> & { readonly _tag: "ParseError";} & Readonly<Problem> { constructor(args: { readonly line: number; readonly message: string; });}Models
The frontmatter schema: a struct over string fields.
Signature
type FrontmatterDefinition = Markdown.IslandDefinitionA recovered document, any valid frontmatter fields, and diagnostics in source order.
Signature
interface LenientParsed extends Parsed { readonly problems: readonly Array<Problem>;}Options for parseMarkdown, parseMarkdownWithFrontmatter,
parseMarkdownLenient and the Vite plugin.
Details
With islands, a directive whose name isn't listed, an attribute its
schema doesn't declare, or a value its schema rejects is a problem.
Without it, any island parses.
With frontmatter, a leading --- block of flat key: value lines is
checked against the schema and left out of the document. Without it, a
frontmatter block is a problem. Strict parsing throws on the first problem;
lenient parsing returns diagnostics and recovers the document.
Signature
interface Options { readonly frontmatter?: IslandDefinition; readonly islands?: Readonly<Record<string, IslandDefinition>>;}A parsed document and its frontmatter fields, when the source has a
frontmatter block and frontmatter is set.
Signature
interface Parsed { readonly document: Document; readonly frontmatter: Option<Readonly<Record<string, string>>>;}One construct that strict parsing rejects: its 1-based line, and a message that names the construct and the line.
Signature
interface Problem { readonly line: number; readonly message: string;}Parsing
parseMarkdown
unstableParses Markdown source into a document. Throws a ParseError on anything
outside the vocabulary, and on frontmatter or islands that fail
options. A frontmatter block is checked and dropped; use
parseMarkdownWithFrontmatter to read it.
See
- parseMarkdownLenient to recover unsupported syntax and collect diagnostics.
Signature
declare function parseMarkdown(source: string, options?: Options): DocumentExample Parsing a post
import { MarkdownParser } from "effect/ui/html"
const document = MarkdownParser.parseMarkdown("# Hello\n\nSome *prose*.")
document.blocks.map((block) => block._tag) // => ["Heading", "Paragraph"]parseMarkdownLenient
unstableParses Markdown source into a document, frontmatter fields and line
diagnostics. Unsupported syntax is recovered as text instead of throwing
a ParseError; invalid option schemas still throw.
When to use
Use when unsupported syntax should not prevent rendering the rest of the document, such as a model's answer parsed repeatedly as it streams.
Details
Recovery preserves surrounding structure. For example, <br> becomes
text inside its table cell. Recovery depends on the construct:
- Raw HTML, inline directives and reference definitions become literal text.
- Reference links, reference images, footnote references and links or images with disallowed URLs retain their source delimiters as text. Markdown inside their labels still parses. Disallowed autolinks become literal text.
- An invalid island becomes a paragraph containing its opening line, followed by its parsed blocks and, if present, a paragraph containing its closing line.
- Without a
frontmatterschema, a frontmatter block is parsed as Markdown and reported as a problem. With a schema, the block is removed from the document; invalid fields produce a diagnostic andfrontmatterisNone.
The document always decodes with the Markdown.Document schema, and raw
HTML is always a Text node, which Markdown.view renders escaped.
problems[0] is the problem parseMarkdown throws for the same source.
Gotchas
Pass islands: {} to recover unregistered directives as text. Without
islands, any valid ::Name directive parses as an island, which renders
nothing unless a view is registered for it.
See
- parseMarkdown for Markdown you author, which fails on the first problem.
Signature
declare function parseMarkdownLenient(input: string, options: Options): LenientParsedExample Parsing a streamed answer
import { MarkdownParser } from "effect/ui/html"
const { document, problems } = MarkdownParser.parseMarkdownLenient( "| Step | Note |\n| - | - |\n| 1 | first<br>second |", { islands: {} })
document.blocks.map((block) => block._tag) // => ["Table"]problems.map((problem) => problem.line) // => [3]parseMarkdownWithFrontmatter
unstableParses Markdown source into a document and its frontmatter fields.
Throws a ParseError on anything outside the vocabulary.
Details
The fields are the strings the block declares, already checked against
options.frontmatter, so decoding them with that schema succeeds.
Signature
declare function parseMarkdownWithFrontmatter(input: string, options?: Options): Parsed