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?

Thanks!

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