# @lezer/markdown

This is an incremental Markdown ([CommonMark](https://commonmark.org/)
with support for extension) parser that integrates well with the
[Lezer](https://lezer.codemirror.net/) parser system. It does not in
fact use the Lezer runtime (that runs LR parsers, and Markdown can't
really be parsed that way), but it produces Lezer-style compact syntax
trees and consumes fragments of such trees for its incremental
parsing.

Note that this only _parses_ the document, producing a data structure
that represents its syntactic form, and doesn't help with outputting
HTML. Also, in order to be single-pass and incremental, it doesn't do
some things that a conforming CommonMark parser is expected to
do—specifically, it doesn't validate link references, so it'll parse
`[a][b]` and similar as a link, even if no `[b]` reference is
declared.

The
[@codemirror/lang-markdown](https://github.com/codemirror/lang-markdown)
package integrates this parser with CodeMirror to provide Markdown
editor support.

The code is licensed under an MIT license.

## Interface

  parser: MarkdownParser

The default CommonMark parser.

    class
    MarkdownParser extends Parser (https://lezer.codemirror.net/docs/ref/#common.Parser)

A Markdown parser configuration.

  nodeSet: NodeSet (https://lezer.codemirror.net/docs/ref/#common.NodeSet)

The parser's syntax node
types (https://lezer.codemirror.net/docs/ref/#common.NodeSet).

  configure(spec: MarkdownExtension) → MarkdownParser

Reconfigure the parser.

  parseInline(text: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), offset: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → Element[]

Parse the given piece of inline text at the given offset,
returning an array of Element objects representing
the inline content.

    interface
    MarkdownConfig

Objects of this type are used to
configure the Markdown parser.

  props⁠?: readonly NodePropSource (https://lezer.codemirror.net/docs/ref/#common.NodePropSource)[]

Node props to add to the parser's node set.

  defineNodes⁠?: readonly (string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | NodeSpec)[]

Define new node types for use in parser extensions.

  parseBlock⁠?: readonly BlockParser[]

Define additional block parsing logic.

  parseInline⁠?: readonly InlineParser[]

Define new inline parsing logic.

  remove⁠?: readonly string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)[]

Remove the named parsers from the configuration.

  wrap⁠?: ParseWrapper (https://lezer.codemirror.net/docs/ref/#common.ParseWrapper)

Add a parse wrapper (such as a mixed-language
parser) to this parser.

  type
  MarkdownExtension = MarkdownConfig | readonly MarkdownExtension[]

To make it possible to group extensions together into bigger
extensions (such as the Github-flavored Markdown
extension), reconfiguration accepts
nested arrays of config objects.

  parseCode(config: Object (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)) → MarkdownExtension

Create a Markdown extension to enable nested parsing on code
blocks and/or embedded HTML.

  config

  codeParser⁠?: fn(info: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)) → Parser (https://lezer.codemirror.net/docs/ref/#common.Parser) | null (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)

When provided, this will be used to parse the content of code
blocks. info is the string after the opening ``` marker,
or the empty string if there is no such info or this is an
indented code block. If there is a parser available for the
code, it should return a function that can construct the
parse (https://lezer.codemirror.net/docs/ref/#common.PartialParse).

  htmlParser⁠?: Parser (https://lezer.codemirror.net/docs/ref/#common.Parser)

The parser used to parse HTML tags (both block and inline).

### GitHub Flavored Markdown

  GFM: MarkdownConfig[]

Extension bundle containing Table,
TaskList, Strikethrough, and
Autolink.

  Table: MarkdownConfig

This extension provides
GFM-style (https://github.github.com/gfm/#tables-extension-)
tables, using syntax like this:

| head 1 | head 2 |
| ---    | ---    |
| cell 1 | cell 2 |

  TaskList: MarkdownConfig

Extension providing
GFM-style (https://github.github.com/gfm/#task-list-items-extension-)
task list items, where list items can be prefixed with [ ] or
[x] to add a checkbox.

  Strikethrough: MarkdownConfig

An extension that implements
GFM-style (https://github.github.com/gfm/#strikethrough-extension-)
Strikethrough syntax using ~~ delimiters.

  Autolink: MarkdownConfig

Extension that implements autolinking for
www./http:///https:///mailto:/xmpp: URLs and email
addresses.

### Other extensions

  Subscript: MarkdownConfig

Extension providing
Pandoc-style (https://pandoc.org/MANUAL.html#superscripts-and-subscripts)
subscript using ~ markers.

  Superscript: MarkdownConfig

Extension providing
Pandoc-style (https://pandoc.org/MANUAL.html#superscripts-and-subscripts)
superscript using ^ markers.

  Emoji: MarkdownConfig

Extension that parses two colons with only letters, underscores,
and numbers between them as Emoji nodes.

### Extension

The parser can, to a certain extent, be extended to handle additional
syntax.

    interface
    NodeSpec

Used in the configuration to define
new syntax node
types (https://lezer.codemirror.net/docs/ref/#common.NodeType).

  name: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

The node's name.

  block⁠?: boolean (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

Should be set to true if this type represents a block node.

  composite⁠?: fn(cx: BlockContext, line: Line, value: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → boolean (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

If this is a composite block, this should hold a function that,
at the start of a new line where that block is active, checks
whether the composite block should continue (return value) and
optionally adjusts the line's base position
and registers nodes for any markers involved
in the block's syntax.

  style⁠?: Tag (https://lezer.codemirror.net/docs/ref/#highlight.Tag) | readonly Tag (https://lezer.codemirror.net/docs/ref/#highlight.Tag)[] | Object (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)<Tag (https://lezer.codemirror.net/docs/ref/#highlight.Tag) | readonly Tag (https://lezer.codemirror.net/docs/ref/#highlight.Tag)[]>

Add highlighting tag information for this node. The value of
this property may either by a tag or array of tags to assign
directly to this node, or an object in the style of
styleTags (https://lezer.codemirror.net/docs/ref/#highlight.styleTags)'s
argument to assign more complicated rules.

    class
    BlockContext implements PartialParse (https://lezer.codemirror.net/docs/ref/#common.PartialParse)

Block-level parsing functions get access to this context object.

  lineStart: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The start of the current line.

  parser: MarkdownParser

The parser configuration used.

  depth: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The number of parent blocks surrounding the current block.

  parentType(depth⁠?: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) = this.depth - 1) → NodeType (https://lezer.codemirror.net/docs/ref/#common.NodeType)

Get the type of the parent block at the given depth. When no
depth is passed, return the type of the innermost parent.

  nextLine() → boolean (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

Move to the next input line. This should only be called by
(non-composite) block parsers that consume
the line directly, or leaf block parser
nextLine methods when they
consume the current line (and return true).

  peekLine() → string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

Retrieve the text of the line after the current one, without
actually moving the context's current line forward.

  prevLineEnd() → number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The end position of the previous line.

  startComposite(type: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), start: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), value⁠?: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) = 0)

Start a composite block. Should only be called from block
parser functions that return null.

  addElement(elt: Element)

Add a block element. Can be called by block
parsers.

  addLeafElement(leaf: LeafBlock, elt: Element)

Add a block element from a leaf parser. This
makes sure any extra composite block markup (such as blockquote
markers) inside the block are also added to the syntax tree.

  elt(type: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), from: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), to: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), children⁠?: readonly Element[]) → Element
elt(tree: Tree (https://lezer.codemirror.net/docs/ref/#common.Tree), at: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → Element

Create an Element object to represent some syntax
node.

    interface
    BlockParser

Block parsers handle block-level structure. There are three
general types of block parsers:

- 
Composite block parsers, which handle things like lists and
blockquotes. These define a parse method
that starts a composite block
and returns null when it recognizes its syntax.

- 
Eager leaf block parsers, used for things like code or HTML
blocks. These can unambiguously recognize their content from its
first line. They define a parse method
that, if it recognizes the construct,
moves the current line forward to the
line beyond the end of the block,
add a syntax node for the block, and
return true.

- 
Leaf block parsers that observe a paragraph-like construct as it
comes in, and optionally decide to handle it at some point. This
is used for "setext" (underlined) headings and link references.
These define a leaf method that checks
the first line of the block and returns a
LeafBlockParser object if it wants to
observe that block.

  name: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

The name of the parser. Can be used by other block parsers to
specify precedence.

  parse⁠?: fn(cx: BlockContext, line: Line) → boolean (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) | null (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)

The eager parse function, which can look at the block's first
line and return false to do nothing, true if it has parsed
(and moved past a block), or null if
it has started a composite block.

  leaf⁠?: fn(cx: BlockContext, leaf: LeafBlock) → LeafBlockParser | null (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)

A leaf parse function. If no regular parse
functions match for a given line, its content will be
accumulated for a paragraph-style block. This method can return
an object that overrides that style of
parsing in some situations.

  endLeaf⁠?: fn(cx: BlockContext, line: Line, leaf: LeafBlock) → boolean (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

Some constructs, such as code blocks or newly started
blockquotes, can interrupt paragraphs even without a blank line.
If your construct can do this, provide a predicate here that
recognizes lines that should end a paragraph (or other non-eager
leaf block).

  before⁠?: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

When given, this parser will be installed directly before the
block parser with the given name. The default configuration
defines block parsers with names LinkReference, IndentedCode,
FencedCode, Blockquote, HorizontalRule, BulletList, OrderedList,
ATXHeading, HTMLBlock, and SetextHeading.

  after⁠?: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

When given, the parser will be installed directly after the
parser with the given name.

    interface
    LeafBlockParser

Objects that are used to override
paragraph-style blocks should conform to this interface.

  nextLine(cx: BlockContext, line: Line, leaf: LeafBlock) → boolean (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

Update the parser's state for the next line, and optionally
finish the block. This is not called for the first line (the
object is constructed at that line), but for any further lines.
When it returns true, the block is finished. It is okay for
the function to consume the current
line or any subsequent lines when returning true.

  finish(cx: BlockContext, leaf: LeafBlock) → boolean (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

Called when the block is finished by external circumstances
(such as a blank line or the start of
another construct). If this parser can handle the block up to
its current position, it should
finish the block and return
true.

    class
    Line

Data structure used during block-level per-line parsing.

  text: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

The line's full text.

  baseIndent: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The base indent provided by the composite contexts (that have
been handled so far).

  basePos: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The string position corresponding to the base indent.

  pos: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The position of the next non-whitespace character beyond any
list, blockquote, or other composite block markers.

  indent: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The column of the next non-whitespace character.

  next: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The character code of the character after pos.

  skipSpace(from: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

Skip whitespace after the given position, return the position of
the next non-space character or the end of the line if there's
only space after from.

  moveBase(to: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))

Move the line's base position forward to the given position.
This should only be called by composite block
parsers or markup skipping
functions.

  moveBaseColumn(indent: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))

Move the line's base position forward to the given column.

  addMarker(elt: Element)

Store a composite-block-level marker. Should be called from
markup skipping functions when they
consume any non-whitespace characters.

  countIndent(to: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), from⁠?: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) = 0, indent⁠?: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) = 0) → number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

Find the column position at to, optionally starting at a given
position and column.

  findColumn(goal: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

Find the position corresponding to the given column.

    class
    LeafBlock

Data structure used to accumulate a block's content during leaf
block parsing.

  parsers: LeafBlockParser[]

The block parsers active for this block.

  start: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The start position of the block.

  content: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

The block's text content.

    class
    InlineContext

Inline parsing functions get access to this context, and use it to
read the content and emit syntax nodes.

  parser: MarkdownParser

The parser that is being used.

  text: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

The text of this inline section.

  offset: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The starting offset of the section in the document.

  char(pos: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

Get the character code at the given (document-relative)
position.

  end: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The position of the end of this inline section.

  slice(from: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), to: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

Get a substring of this inline section. Again uses
document-relative positions.

  addDelimiter(type: DelimiterType, from: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), to: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), open: boolean (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean), close: boolean (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)) → number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

Add a delimiter at this given position. open
and close indicate whether this delimiter is opening, closing,
or both. Returns the end of the delimiter, for convenient
returning from parse functions.

  hasOpenLink: boolean (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

Returns true when there is an unmatched link or image opening
token before the current position.

  addElement(elt: Element) → number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

Add an inline element. Returns the end of the element.

  findOpeningDelimiter(type: DelimiterType) → number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) | null (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)

Find an opening delimiter of the given type. Returns null if
no delimiter is found, or an index that can be passed to
takeContent otherwise.

  takeContent(startIndex: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → Element[]

Remove all inline elements and delimiters starting from the
given index (which you should get from
findOpeningDelimiter,
resolve delimiters inside of them, and return them as an array
of elements.

  skipSpace(from: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

Skip space after the given (document) position, returning either
the position of the next non-space character or the end of the
section.

  elt(type: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), from: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), to: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), children⁠?: readonly Element[]) → Element
elt(tree: Tree (https://lezer.codemirror.net/docs/ref/#common.Tree), at: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → Element

Create an Element for a syntax node.

    interface
    InlineParser

Inline parsers are called for every character of parts of the
document that are parsed as inline content.

  name: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

This parser's name, which can be used by other parsers to
indicate a relative precedence.

  parse(cx: InlineContext, next: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), pos: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) → number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The parse function. Gets the next character and its position as
arguments. Should return -1 if it doesn't handle the character,
or add some element or
delimiter and return the end
position of the content it parsed if it can.

  before⁠?: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

When given, this parser will be installed directly before the
parser with the given name. The default configuration defines
inline parsers with names Escape, Entity, InlineCode, HTMLTag,
Emphasis, HardBreak, Link, and Image. When no before or
after property is given, the parser is added to the end of the
list.

  after⁠?: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

When given, the parser will be installed directly after the
parser with the given name.

    interface
    DelimiterType

Delimiters are used during inline parsing to store the positions
of things that might be delimiters, if another matching
delimiter is found. They are identified by objects with these
properties.

  resolve⁠?: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

If this is given, the delimiter should be matched automatically
when a piece of inline content is finished. Such delimiters will
be matched with delimiters of the same type according to their
open and close properties. When a
match is found, the content between the delimiters is wrapped in
a node whose name is given by the value of this property.

When this isn't given, you need to match the delimiter eagerly
using the findOpeningDelimiter
and takeContent methods.

  mark⁠?: string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

If the delimiter itself should, when matched, create a syntax
node, set this to the name of the syntax node.

    class
    Element

Elements are used to compose syntax nodes during parsing.

  type: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The node's
id (https://lezer.codemirror.net/docs/ref/#common.NodeType.id).

  from: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The start of the node, as an offset from the start of the document.

  to: number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)

The end of the node.

