Get all the headings inside a markdown document

I want to extract the ATXHeadings from inside a markdown document, then have the ability to extract the corresponding value from the document’s state as well.(tl;dr have a highlight of all my headings inside my document)

If tried using syntaxTree(state), then for each child, filtering them by type(whilst storing their original position in the tree), and finally, based on the position, get the value from the doc… but i m not sure if this is the best way to do it. Should i be concerned about potential performance issues(have to take the AST and extract the headings each time the document updates)? Can i do it in another, say, better way?


Lezer System Guide describes this, specifically, the Tree Cursors, seems to do the trick.

Having the same challenge right now. Need to extract all Markdown headlines from a doc. On load and after each update.

Is getting a cursor, iterating over all nodes, checking for type and then fetching the actual value really the way to go? Seems a bit costly, especially if it’s done on every update.