QuoteMark's parent in lezer-markdown

It’s possible for QuoteMark’s parent not to be a Blockquote, which makes it more difficult to associate a QuoteMark with its corresponding Blockquote when nested:

import { parser } from "@lezer/markdown";

const tree = parser.parse('> > a\n> > b')
tree.iterate({
  enter: (nodeRef) => {
    let { node } = nodeRef
    const ancestors = []
    while (node.parent) {
      ancestors.push(node.parent.name)
      node = node.parent
    }
    console.log(nodeRef.name, ancestors)
  }
})

Output:

Document []
Blockquote [ 'Document' ]
QuoteMark [ 'Blockquote', 'Document' ]
Blockquote [ 'Blockquote', 'Document' ]
QuoteMark [ 'Blockquote', 'Blockquote', 'Document' ]
Paragraph [ 'Blockquote', 'Blockquote', 'Document' ]
QuoteMark [ 'Paragraph', 'Blockquote', 'Blockquote', 'Document' ]
QuoteMark [ 'Paragraph', 'Blockquote', 'Blockquote', 'Document' ]

This behavior seems to arise from the nodes being strictly nested, with Paragraph spanning multiple lines. Is it a requirement that the nodes are nested this way, or could it be changed so that QuoteMark always has Blockquote as a parent?

Lezer requires a tree to be one-dimensional, with each node being entirely inside its parent. Markdown’s structure is pretty weird, and doesn’t completely fit this. So yeah, quote marks can be inside paragraphs, and that will not be changed.

1 Like

Makes sense; thank you for the explanation.

I was trying to manually match the marks to their blocks from the syntax tree when I ran into this:

import { parser } from "@lezer/markdown";

const tree = parser.parse('>     a\n>')
tree.iterate({
  enter: (nodeRef) => {
    let { node } = nodeRef
    const ancestors = []
    while (node.parent) {
      ancestors.push(node.parent.name)
      node = node.parent
    }
    console.log(nodeRef.name, nodeRef.from, nodeRef.to, ancestors)
  }
})
Document 0 9 []
Blockquote 0 9 [ 'Document' ]
QuoteMark 0 1 [ 'Blockquote', 'Document' ]
CodeBlock 6 7 [ 'Blockquote', 'Document' ]
CodeText 6 7 [ 'CodeBlock', 'Blockquote', 'Document' ]
QuoteMark 8 9 [ 'Document' ]

Should the last QuoteMark have Document as a parent here instead of Blockquote?

Oh, that was a bug. This patch should help.

1 Like