Skip to main content

tabnas_markdown/
commonmark.rs

1/* Copyright (c) 2021-2026 Richard Rodger, MIT License */
2
3//! Engine-free entry point for the CommonMark parser. Port of
4//! `ts/src/commonmark.ts` and `go/commonmark.go`.
5//!
6//! Nothing reachable from here imports the tabnas engine. That keeps the
7//! parser testable (and the conformance suite runnable) without the engine
8//! present, exactly as in the other two runtimes. The plugin wiring lives
9//! in `lib.rs`, `engine_block.rs` and `engine_inline.rs`, which import
10//! this; never the other way round.
11
12use crate::block::parse_blocks;
13use crate::inline::{parse_inlines, parse_inlines_keeping_references};
14use crate::node::Tree;
15use crate::options::Options;
16
17/// The full two-phase parse (spec Appendix A): block structure first, then
18/// inlines over the accumulated string content of each leaf, using the
19/// link reference map the block phase collected.
20pub fn parse(input: &str, options: Options) -> Tree {
21    let (mut tree, refmap) = parse_blocks(input, options);
22    parse_inlines(&mut tree, refmap, options);
23    tree
24}
25
26/// [`parse`] with the inline phase reading every bracketed label a
27/// definition could name as a reference, left unresolved, and the link
28/// reference map not consulted: the tree the AST is projected from (see
29/// `ast.rs`). [`parse`] resolves references as CommonMark says, which is
30/// the tree the HTML renderer needs.
31pub fn parse_keeping_references(input: &str, options: Options) -> Tree {
32    let (mut tree, _) = parse_blocks(input, options);
33    parse_inlines_keeping_references(&mut tree, options);
34    tree
35}