Skip to content

syntax/lezer/lezer-commonmark

Where @lezer/markdown 1.8 reads a document differently from CommonMark 0.31.2, and what this package does about it.

commonMarkFixes corrects three of its rules, and every parser of this package includes it, or, with the htmlBlock quirk, commonMarkFixesWithoutHtmlBlock:

  • An ATX heading’s # sequence may be followed by a tab, not only by a space. - <textarea> opens an HTML block of the first kind, beside <pre>, <script> and <style>, and any of their four end tags closes one. - Raw inline HTML follows the spec: a tag name right after its <, an attribute only after whitespace, and the comment forms of 0.31.2 (<!-->, <!--->, and -- inside a comment).

The fourth difference is by design: to parse incrementally, @lezer/markdown does not check that a link reference is defined, so it reads [foo] as a link even when no [foo]: definition exists, and that changes how brackets and emphasis around it pair up. linkReferenceCheck makes it check, given the labels a document defines, and parseWithLinkReferences parses a whole document that way, twice. A tree parsed with the check cannot be reused incrementally once an edit adds or removes a definition, so the editor’s parser leaves it out.

The heading and HTML block parsers are adapted from @lezer/markdown’s own, under its MIT license (see THIRD-PARTY-NOTICES.md).

Function Description
collectLinkReferenceLabels The labels of the link reference definitions in a tree, normalized as CommonMark compares them.
linkReferenceCheck Makes link references links only when their label is defined: a [text], [text][] or [text][label] whose label is not among labels stays text, with its brackets.
parseWithLinkReferences Parses a whole document with link references checked: once to find the labels it defines, and again with linkReferenceCheck.
Variable Description
commonMarkFixes Corrections that bring @lezer/markdown 1.8 closer to CommonMark 0.31.2.
commonMarkFixesWithoutHtmlBlock commonMarkFixes without the HTML block of the first kind, for a parser that reads HTML blocks its own way.