I started to use markdown to take notes.
I use marked to view my markdown notes and its beautiful.
But as my notes get longer I find it diff
You can generate it using this bash one-liner. Assumes your markdown file is called FILE.md
.
echo "## Contents" ; echo ;
cat FILE.md | grep '^## ' | grep -v Contents | sed 's/^## //' |
while read -r title ; do
link=$(echo $title | tr 'A-Z ' 'a-z-') ;
echo "- [$title](#$link)" ;
done
On Gitlab, markdown supports this : [[_TOC_]]
I wrote a python script that parses a markdown file and outputs a table of contents as a markdown list: md-to-toc
Unlike other scripts I've found, md-to-toc correctly supports duplicate titles. It also doesn't require an internet connection, so it works on any md file, not just those available from a public repo.
# Table of Contents
1. [Example](#example)
2. [Example2](#example2)
3. [Third Example](#third-example)
## Example [](#){name=example}
## Example2 [](#){name=example2}
## [Third Example](#){name=third-example}
If you use markdown extra, don't forget you can add special attributes to links, headers, code fences, and images.
https://michelf.ca/projects/php-markdown/extra/#spe-attr
MultiMarkdown 4.7 has a {{TOC}} macro that inserts a table of contents.
I just coded an extension for python-markdown
, which uses its parser to retrieve headings, and outputs a TOC as Markdown-formatted unordered list with local links. The file is
... and it should be placed in markdown/extensions/
directory in the markdown installation. Then, all you have to do, is type anchor <a>
tags with an id="..."
attribute as a reference - so for an input text like this:
$ cat test.md
Hello
=====
## <a id="sect one"></a>SECTION ONE ##
something here
### <a id='sect two'>eh</a>SECTION TWO ###
something else
#### SECTION THREE
nothing here
### <a id="four"></a>SECTION FOUR
also...
... the extension can be called like this:
$ python -m markdown -x md_toc test.md
* Hello
* [SECTION ONE](#sect one)
* [SECTION TWO](#sect two)
* SECTION THREE
* [SECTION FOUR](#four)
... and then you can paste back this toc in your markdown document (or have a shortcut in your text editor, that calls the script on the currently open document, and then inserts the resulting TOC in the same document).
Note that older versions of python-markdown
don't have a __main__.py
module, and as such, the command line call as above will not work for those versions.