markdown-toc provides a simple-minded command-line utility to provide a table-of-contents to a (GFM) Markdown document.
All of the heavy lifting is done by the "goldmark" CommonMark parser and the "toc" extension.
This is lightly tested, and generates marginally adequate HTML (no CSS, so its output is very 1994).