v2.0.11

Parsing Pipeline

Last updated


This page covers the order in which Parse() classifies each line, how inline parsing masks content, and how footnotes and reference links resolve at the end.

Block Order


ParseLines reads each line from top to bottom and checks the types in this order; the first match wins:
OrderBlockRule
1Blank lineWhitespace only; consecutive blank lines merge into one break
2Code blockStarts with 3 or more ` or ~
3Raw HTMLA line that starts with an HTML tag
4TableContains | and the next line is a separator row
5Horizontal rule***, ---, ___ (spaces allowed)
6BlockquoteStarts with >, including GitHub alerts
7Footnote definition[^id]:, checked at the top level only
8Link definition[label]: url "title", produces no node
9ListStarts with -, * or 1.
10Heading# to ######, or Setext === / ---
11ParagraphEverything else; adjacent lines merge into one paragraph joined by a "\n" text node

The content of blockquotes, list items and footnotes calls ParseLines recursively, so any block can appear inside them.

Inline Parsing


StepContent
1Replace code spans in the whole line with placeholders first; a file path inside one becomes a file link
2Replace inline math and escapes with placeholders
3Replace footnote references, links, images, paths and bare URLs with placeholders too
4Match bold, italic, strikethrough, highlight, subscript and superscript
5Restore the placeholders to their nodes

Placeholders keep _ and * inside links from being read as styles, and let styles that wrap links or code such as **[text](url)** and **bold `code`** parse correctly.

Post-Processing


Nodes that need the whole document are resolved after parsing finishes:
ItemRule
FootnotesNumbered by first reference, with a footnotes block appended at the end; references without a definition are restored as [^id] text
Reference linksReplaced with link nodes from the link definitions (case-insensitive); undefined ones are restored as [label][ref] text

  1. <div id="out" class="nanomd"></div>
  2. <script>
  3. const md = "First line\nsecond line\n\n\n\nSee [docs][d] and a note[^1].\n\n[d]: https://nanomd.pardn.io\n[^1]: Footnote text.";
  4. const ast = MDParser.Parse(md);
  5. console.log(ast.map((node) => node.type).join(", "));
  6. document.getElementById("out").innerHTML = ast.html();
  7. </script>
Live preview · @pardnchiu/nanomd@2.0.11
中文