If you look here: http://en.wikipedia.org/wiki/Stack_Overflow
You\'ll notice there\'s a little \"Content\" section, if you click on one of the links, it will send you to
Due to the fact that github has it's own way of generating id=".." attributes in h1, h2, h3, etc... headers in html version after processing Markdown (for example Bitbucket use little different pattern of sluggifying headers title to id="slug") it is handy to don't reinvent the wheel and use library that reverse engineered this process.
I found one quite good library for this task called markdown-toc.
For me it seems the best solution because I always have installed node on my machine.
Just execute npx markdown-toc -i file.md.
And it looks like it is one of more popular tools for this task - at least in node.js ecosystem.
ls
cat <<EOF >> test.md | tee
## Table of Contents
<!-- toc -->
- old toc 1
- old toc 2
- old toc 3
<!-- tocstop -->
## abc
This is a b c.
## xyz
This is x y z.
EOF
ls
cat test.md
npx markdown-toc -i test.md
cat test.md
output:
https://github.com/jonschlinkert/markdown-toc
git clone your-repo.wiki.git
(add the .wiki
right before .git
to clone the wikinpm i -g markdown-toc
<!-- toc -->
(case sensitive) in your wiki's markdownmarkdown-toc -i my-wiki-markdown.md
(-i
will edit it in place)Update: I think maybe https://github.com/thlorenz/doctoc is more popular now.
If you happen to use Visual Studio Code, there is easy-to-use extension called Markdown All in One that can make the TOC for any .md file in an instant.
Just open Command Palette (Ctrl-Shift-P
) -> Markdown: Create Table of Contents
As an additional tip, you might want to turn the "automatic TOC updates on save" OFF by using
"markdown.extension.toc.updateOnSave": false,
in your Visual Studio Settings (Command Palette -> Preferences: Open Settings (JSON)).