I found the answer by M. Geiger very helpful.

First, you need to have pandoc installed.

Let’s say you have this markdown file: README.md. To generate table of contents for this file, first go to its directory, then run the following in your Terminal:

pandoc -s –toc README.md -o README-TOC.md
  • …