Expand description
Native CommonMark node tree, the internal parse result. Port of
ts/src/node.ts and go/node.go; keep the three in step.
The canonical tree is linked (parent / firstChild / lastChild / prev / next) rather than a children array, because both phases of the CommonMark algorithm (spec 0.31.2 Appendix A) splice nodes mid-walk: the block phase keeps a spine of open blocks and closes them from the tail, and the inline phase’s delimiter stack rewrites sibling runs in place.
This port keeps exactly those links, but the nodes live in an arena
(Tree) and the links are indices (NodeId) rather than pointers.
That is the one shape difference from the other two runtimes, and it is
what Rust’s ownership rules ask for: a node that points at its parent
and its siblings cannot be a plain owned value, and reference counting
would make every splice a borrow-checker negotiation. An arena gives
the same O(1) splices with plain integer bookkeeping, and an unlinked
node simply becomes unreachable. A driver that writes blocks as they
finish and keeps no tree gives a written block’s slots back
(Tree::free_subtree, crate::block::BlockParser::release_block),
which the next nodes fill, so a long document’s arena stays as large as
its widest open block, not its length (tabnas/markdown#100).
The public, mdast-adjacent AST is produced from this tree by ast.rs.
That projection is lossy on purpose (it drops raw inline HTML boundaries
and source positions); the tree is what html.rs renders for CommonMark
conformance.
Structs§
- Definition
- A link reference definition (section 4.7) as the block phase read it from the front of a paragraph.
- Definition
Site - Where a definition stands among a container’s children: after the
child
after(None: before the first), in source order. Definitions are not blocks in the spec’s algorithm, which reads them out of a paragraph and drops a paragraph that held nothing else, so they are kept beside the children rather than among them: the block phase’s own reads of a container’s children (list looseness, the blank-line flags) never see one. - List
Data - What sections 5.2 and 5.3 need to decide list identity and rendering.
- MdNode
- One node of the tree.
- Tree
- The arena holding one document’s nodes, with the document at
Tree::root. - Walk
Event - One step of a depth-first walk.
- Walker
- A depth-first walker over the subtree rooted at
root. It holds ids only, so the tree may be mutated between steps as long as the node the walker stands on stays linked, which is what both parse phases rely on.
Enums§
- Align
- Per-column alignment of a GFM table, from the colons in its delimiter
row:
:--left,--:right,:-:center. A column with no colon isNone, which is the canonical runtime’snulland the Go port’sAlignNone. - List
Type - Bullet or ordered.
- Node
Type - The node kinds. String-valued in the other two runtimes so that debug
output and the AST projection read the same;
NodeType::as_stris the same spelling here. - Reference
Type - How a reference names its label (section 6.3):
[a][b],[a][]or[a].
Type Aliases§
- NodeId
- An index into a
Tree’s arena. - Source
Pos [[startLine, startCol], [endLine, endCol]], all 1-based, with columns counted in characters (not bytes) as the spec counts them.- Table
Align - One column’s alignment;
Nonefor a delimiter cell with no colon.