Markdown parser, AST and writer for mq. It is the parser behind every mq query, and works as a standalone library.
- CommonMark and GFM, plus frontmatter, math and MDX
- Parses to a typed node tree with source positions, and writes it back to Markdown, HTML, plain text or JSON
- Optional Obsidian syntax (
[[wikilinks]],![[embeds]],> [!callouts]) and HTML to Markdown conversion
Spec conformance
Every example of each spec is rendered to HTML with to_html and compared with the expected HTML in the spec (just test-spec).
| Spec | Passed / Total | Rate |
|---|---|---|
| GFM spec (based on CommonMark 0.29) | 625 / 648 | 96.5% |
| CommonMark 0.31.2 | 637 / 652 | 97.7% |
GFM extensions (extensions.txt) |
26 / 30 | 86.7% |
The examples that do not match are intentional differences, not parse failures:
- Frontmatter: a document that starts with
---(YAML) or+++(TOML) has frontmatter. Turn it off withParseOptions::with_frontmatter(false). - Nested
strong: runs of*and_nest as in CommonMark 0.30 and later. - Link protocols: a destination with a protocol other than
http,https,irc,ircs,mailtoorxmppgets nohref. - Autolink literals: GFM extended autolinks (
www.example.com) that the base spec does not have. - Tag filter:
<script>,<style>and<textarea>are written as text.
Nesting is bounded (128 levels for block containers and inline emphasis), so a hostile document cannot exhaust the stack.
Performance
Rough numbers from a --release build, median of 31 runs for the small inputs and 7 for the large one. The parse column is str::parse::<Markdown>(), to_html renders an already parsed document, and to_string writes it back as Markdown.
| Input | Size | Nodes | parse | to_html | to_string |
|---|---|---|---|---|---|
| CommonMark spec | 205 KB | 2,098 | 1.4 ms (148 MB/s) | 2.5 ms | 0.8 ms |
mq documentation (all .md) |
298 KB | 5,528 | 2.6 ms (117 MB/s) | 5.7 ms | 2.0 ms |
| the same documentation, 20 times over | 5.9 MB | 110,560 | 55 ms (108 MB/s) | 116 ms | 42 ms |
The cost grows linearly with the size of the document. Parse time is on par with comrak.
Install
[]
= "0.9"
Usage
Parse and render
use ;
let doc = "# Title\n\nSome *emphasis*."..unwrap;
println!; // <h1>Title</h1> ...
println!; // plain text of every node
println!; // back to Markdown
// One-shot Markdown to HTML
assert_eq!;
The node tree
doc.nodes is a flat Vec<Node> of the top-level blocks. A node has a name() ("h1", "code_inline", "table_cell"), a value(), children() and a position() with one-based line and column.
use Markdown;
let doc = "# A\n\n[link](http://example.com)"..unwrap;
for node in &doc.nodes
// h1 at 1:1
// link at 3:1
Frontmatter is a node of its own (yaml or toml). GFM tables are a sequence of table_cell and table_align nodes, and task list items are list nodes with a checked state.
Node is an enum with a struct per kind (Heading, Code, Link, List, TableCell, Math, Footnote, MdxJsxFlowElement, ...), so you can also match on it. NodeKind lists every kind with its attributes (AttrSpec), which is what the mq type checker uses.
Frontmatter
use ;
let src = "---\ntitle: Hi\n---\n";
// Default: YAML between `---` lines (TOML between `+++`) is frontmatter.
let doc = src..unwrap;
assert_eq!;
// Off: the lines are a rule and a heading, as in plain CommonMark.
let doc = from_markdown_str_with.unwrap;
assert_eq!;
Rendering options
A line break inside a table cell is written as <br> so the row stays on one line. Newlines in inline code become spaces.
RenderOptions controls how the document is written back as Markdown: the list bullet (-, * or +), how link destinations are wrapped (<url> or bare), how titles are quoted and whether table cells are padded to a common column width (table_padding, on by default). The default keeps what each element had in the source.
use ;
let mut doc = "* a\n* b"..unwrap;
doc.set_options;
assert_eq!;
MDX
Markdown::from_mdx_str parses JSX elements, {expressions} and import/export. A syntax error is a miette diagnostic with the message and position, for example an unclosed <A> reports Expected a closing tag for `<A>` (1:1) before the end of its container.
use Markdown;
let doc = from_mdx_str.unwrap;
// mdx_js_esm, mdx_jsx_flow_element
When writing an MDX document back, text that MDX would read as an expression or ESM is escaped.
Features
| Feature | Description |
|---|---|
json |
Markdown::to_json() and serde support for the tree |
html-to-markdown |
convert_html_to_markdown and Markdown::from_html_str |
color |
to_colored_string() renders with ANSI colors and a ColorTheme |
wikilink |
[[Page]] and [[Page|alias]] as WikiLink nodes |
embed |
![[file.png]] as Embed nodes |
callout |
> [!note]- Title as Callout nodes |
obsidian |
wikilink, embed and callout together |
None is on by default. Wikilinks, embeds and callouts also render to HTML with Obsidian's classes (internal-link, callout). Link targets are used as written.
HTML to Markdown
use ;
let md = convert_html_to_markdown.unwrap;
assert_eq!;
ConversionOptions can turn <title> into an h1, emit YAML frontmatter from <head>, keep <script> contents as code blocks, and resolve relative URLs against base_url (or <base href>).
Documentation
License
MIT