How to extract the document structure programmatically?

In an active sibling topic here, The good, bad, and complicated of writing a PhD thesis in typst TheZoq2 links to their blog and they use a custom conversion tool (Typst to Latex), might be worth just checking what it can do, too.