Markdown has become quite the standard for static contents like blog posts, notes, readmes, etc as popularized by static site generators (SSGs), with a plethora of packages that can parse and transform markdown content into HTML for rendering. Most content written in markdown have multiple layers of headings, hence it is often required that they be rendered alongside a table of contents (hierarchy of headings) to ease navigation through the content.
Consider a blog being developed using one of the popular SSGs for which all blog posts are written in markdown and a parser is used to transform all markdown content to flat (no nested elements) HTML markup for rendering. Given a markdown that has been transformed to HTML, you are required to build a hierarchy of headings that can be used to render a table of contents alongside the content of the markdown.
Since the HTML markup has a flat structure, it is easy to get all the heading elements (`h1