Hongtao Hao / 2021-06-06

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

• -s means “standalone”.

• --toc means including an automatically generated table of contents in the output document.

• -o means “output”.

You may also include --toc-depth=NUMBER. NUMBER here can be anything from 1-6. The default is 3, which means level-1, 2, and 3 headings will be included in the table of contents.