Pandoc is the Swiss army knife of markup. It can add a GitHub-compatible table of contents to a markdown file called README.md
with this command:
pandoc --from markdown --to markdown --table-of-contents --standalone README.md
The --from
and --to
options are required, and --table-of-contents
requires --standalone
. The options have short versions, so this is equivalent:
pandoc -f markdown -t markdown --toc -s README.md
By default the command prepends the table of contents and writes the result to stdout. Since you probably only want the table of contents itself, you might want to pipe to a pager to see the top of the file. For example:
pandoc -f markdown -t markdown --toc -s README.md | less
Other useful options are:
--toc-depth=NUMBER
to specify the heading levels to include. The default is 3.
--output=FILE
to write to a file instead of stdout.
See https://pandoc.org for installation and more usage instructions.