CommonMark 0.31 block-phase parser — paragraphs, lists, GFM tables, link refs.
Module blockParser | Source packages/front/office/md/src/block/parser.js | Deps mdErrors, mdNode, mdAstTypes, refsLinkRefs, mdBlockHtmlPatterns, mdBlockLinkRef, mdBlockListData, mdBlockTable, mdBlockTaskList, mdBlockTypes, mdBlockStarts, mdBlockCursor | Worker-safe yes
The block-structure phase of parsing (CommonMark appendix "A parsing strategy"): tokenizes the text line by line into blocks (CommonMark §4) — paragraphs, headings, thematic breaks, code blocks, HTML blocks, block quotes, lists, tables (GFM). Detects and extracts link reference definitions at finalize time. Inline parsing is delegated to inlineParser via the inlineParser hook passed in options.
blockParser is a pure orchestrator over the sibling mdBlock* sub-modules, each promoted to its own factory descriptor and listed above as a dependency (mdBlockHtmlPatterns, mdBlockLinkRef, mdBlockListData, mdBlockTable, mdBlockTaskList, mdBlockTypes, mdBlockStarts, mdBlockCursor).
Resolve
import { runtime } from '@awacloud/fw';
import { fw_require, modules } from '@awacloud/md';
runtime.registerAll(fw_require);
runtime.registerAll(modules);
const bp = runtime.resolve('blockParser');
// { parse }
API
| Method | Signature | Returns |
|---|---|---|
parse |
(text: string, opts?) => {document, refmap} |
AST root + link refs map |
parse options
| Option | Type | Default | Description |
|---|---|---|---|
inlineParser |
(blockNode, refmap) => void |
none | Hook called on each paragraph / heading / table-cell to parse inlines |
maxDepth |
number |
Infinity (no limit) |
Deepest block nesting allowed (the document is depth 0, its direct blocks depth 1). A container (list, item, quote) that would open deeper throws md/limit-exceeded with context: { kind: 'maxDepth', limit, depth } as it opens, before the deeper tree is built. md.parse passes its own maxDepth here (default 1000) |
parse reads no other option: every block node carries a sourcepos already, and the inline sourcepos / smart / smartDashes / extendedAutolinks options belong to the inline parser (bound through md.createMd(opts)). A maxDepth that is not a number >= 0 is ignored (no limit).
Examples
Standalone (without an inline parser)
const bp = runtime.resolve('blockParser');
const { document, refmap } = bp.parse('# Hi\n\n[foo]: /url');
document.firstChild.type; // 'heading'
refmap.FOO.destination; // '/url'
Wired with the inline parser (the md.js pattern)
const bp = runtime.resolve('blockParser');
const ip = runtime.resolve('inlineParser');
const { document } = bp.parse('# Hi *there*', { inlineParser: ip.parse });
Notes
- Link reference definitions are detected while finalizing paragraphs — not in the inline phase (per spec §4.7). A label may hold a backslash just before a line end (
[\thenfoo]: /uri). A definition whose next line starts with an empty title and goes on with text ([a]: /uthen""text) keeps the definition and drops the title (a title followed by text is not part of the definition); the text line stays a paragraph. - Every U+0000 (NUL) in the input is replaced with U+FFFD, as CommonMark §2.3 requires, whatever its line holds; the replacement takes one UTF-16 unit like the NUL, so source positions do not move.
- Parsing is linear in the input for deeply nested lists (
- - - … x), for blank lines after a deep list and for themaxDepthcheck. - Full lazy-continuation handling: a paragraph can continue across multiple lines even inside block quotes / list items.
- GFM tables are parsed if the line following the header is a valid separator row (
| --- | :--- | ---: |). - The parser is split into this orchestrator plus the
mdBlock*sub-modules listed above as dependencies.
See also
inline/parser— the inline phase, wired via optionsrefs/link-refs— map exposed ondocument.data.refmapast/types— produced typesmd— full wiring with inline