Skip to main content

A utility that is able to generate a table of contents for a markdown file.

Project description

PyPI md-toc version PyPI - License PyPI - Python Version Downloads Dependent repos (via pre-commit Buy me a coffee

Automatically generate a compliant table of contents for a markdown file to improve document readability.



The table of contents (a.k.a: TOC) generated by this program is designed to work with several markdown parsers such as the ones used by GitHub and GitLab.

When used with the in-place option this script will write the TOC at the first occurrency of a marker. The default marker is <!--TOC-->, which, being an HTML comment, will result invisible after the markdown file has been translated.

By default titles up to three indentation levels (in HTML: h1, h2, h3) will be included in the TOC but the user can decide to keep all possible levels.

md_toc makes it is possible to generate ordered and unordered TOCs. In both cases each element of the TOC is by default a link to a paragraph in the web page. It is also possible to generate a non-linked version of the TOC.

If the user wants it, there is the possibility to ignore space indentations within the TOC and to skip an initial number of lines from the markdown file.

Rules for generating the TOC are determined by the selected markdown parser. md-toc aimes infact to be as conformant as possible in respect to each one of them. This was possible by studying the available documentations and by reverse engineering the source codes.


Please read carefully the Markdown specification section of the documentation to learn how this program parsers markdown files and builds a correct output.

API examples

md-toc has a public API. This means for example that you can you easily build a TOC within another Python program. The easiest way to build one for a markdown file is:

>>> import md_toc
>>> f = open('')
>>> print(, end='')
# this
## is
## a
### foo
#### booo
### foo
## file

## bye

# bye
>>> print(md_toc.build_toc(''), end='')
- [this](#this)
  - [is](#is)
  - [a](#a)
    - [foo](#foo)
    - [foo](#foo-1)
  - [file](#file)
  - [bye](#bye)
- [bye](#bye-1)

CLI Helps

$ md_toc --help
$ md_toc github --help
$ md_toc redcarpet --help


Copyright (C) 2017-2021 frnmst (Franco Masotti) <>

md-toc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

md-toc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with md-toc. If not, see <>.

Changelog and trusted source

You can check the authenticity of new releases using my public key.

Changelogs, instructions, sources and keys can be found at

Crypto donations

  • Bitcoin: bc1qnkflazapw3hjupawj0lm39dh9xt88s7zal5mwu
  • Monero: 84KHWDTd9hbPyGwikk33Qp5GW7o7zRwPb8kJ6u93zs4sNMpDSnM5ZTWVnUp2cudRYNT6rNqctnMQ9NbUewbj7MzCBUcrQEY
  • Dogecoin: DMB5h2GhHiTNW7EcmDnqkYpKs6Da2wK3zP
  • Vertcoin: vtc1qd8n3jvkd2vwrr6cpejkd9wavp4ld6xfu9hkhh0

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for md-toc, version 8.0.1
Filename, size File type Python version Upload date Hashes
Filename, size md_toc-8.0.1-py3-none-any.whl (44.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size md_toc-8.0.1.tar.gz (42.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page