Skip to main content

Markdown Table Of Contents

md-toc logo

PyPI md-toc version Debian 13 package nixpkgs unstable package Anaconda.org Downloads Dependent repos (via libraries.io) pre-commit Buy me a coffee

Automatically generate and add an accurate table of contents to markdown files.

Video

image

Description

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.

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.

GitHub and GitLab have introduced their version of the markdown TOC after md-toc and similar tools were created:

  • in March 2021 GitHub added an interactive TOC button at the top-left of readme files. This system works for markdown and others
  • GitLab added an extension called Table of contents to its Gitlab Flavored Mardown

However, md-toc has unique features and is used in systems without automatic TOCs.

Features

  • works offline
  • edits file in place using a TOC marker (default <!--TOC-->) or output to standard output
  • maximum heading level selection (1 to 6)
  • list indentation based on heading, which can optionally be disabled
  • outputs an ordered or unordered TOC list with list marker selection
  • creates anchor links to markdown headings by default or a plain list as alternative
  • checks if heading level is coherent: this avoid creating an erroneous TOC. This feature can be disabled if needed
  • skip any number lines before generating the TOC
  • can read content from standard input
  • handles multiple files at once
  • selection of newline string
  • check if there is difference between existing TOC in file and newly generated one
  • supports GitHub, GitLab, Commonmark, Redcarpet and others
  • pre-commit md-toc hook
  • deterministic output: no AI features, no hallucinations, no tokens, no costs

And more! See the feature comparison table

Examples

You can use md-toc in your blog, documentation based on markdown, GitHub pages, markdown files in Nextcloud, etc...

I use it in my Jekyll-based blog along with its pre-commit hook. I also use it in most repositories where README.md files are present.

Most markdown renderers do not provide a way to automatically generate a TOC so md-toc is useful for this purpose.

A very common use case is this:

$ cat foo.md

# Table of contents

<!--TOC-->

# this
## is
## a
### foo
#### booo
### foo
## file

## bye

# bye

$ md_toc --in-place github --header-levels 6 foo.md        # or: md_toc -p github foo.md
$ cat foo.md

# Table of contents

<!--TOC-->

- [Table of contents](#table-of-contents)
- [this](#this)
  - [is](#is)
  - [a](#a)
    - [foo](#foo)
      - [booo](#booo)
    - [foo](#foo-1)
  - [file](#file)
  - [bye](#bye)
- [bye](#bye-1)

<!--TOC-->

# this
## is
## a
### foo
#### booo
### foo
## file

## bye

# bye

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('foo.md')
>>> print(f.read(), end='')
# this
## is
## a
### foo
#### booo
### foo
## file

## bye

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

You can also write the TOC in place:

>>> import md_toc
>>> f = open('foo.md')
>>> print(f.read(), end='')
# Table of contents

<!--TOC-->

# this
## is
## a
### foo
#### booo
### foo
## file

Test

## bye

# bye
>>> toc = md_toc.api.build_toc('foo.md')
>>> md_toc.write_string_on_file_between_markers('foo.md', toc, '<!--TOC-->')
>>> f = open('foo.md')
>>> print(f.read(), end='')
# Table of contents

<!--TOC-->

- [Table of contents](#table-of-contents)
- [this](#this)
  - [is](#is)
  - [a](#a)
    - [foo](#foo)
      - [boo](#boo)
    - [foo](#foo-1)
  - [file](#file)
  - [bye](#bye)
- [bye](#bye-1)

<!--TOC-->

# this
## is
## a
### foo
#### booo
### foo
## file

Test

## bye

# bye

Quickstart

Distribution packages

Distro packages exist for Arch Linux, Debian, Ubuntu and Nix. These, however are usually not up-to-date. See:

Arch Linux (AUR)

Debian

Nix

One minute setup

  1. install pipx

  2. install md-toc

    pipx install md-toc
    
  3. run md-toc

    md_toc --in-place github --header-levels 6 README.md
    

Safe install

  1. import my GPG public key:

    curl https://blog.franco.net.eu.org/pubkeys/pgp_pubkey_since_2019.txt | gpg --import
    
  2. check its fingerprint and compare it with the one stored on DNS:

    gpg --list-keys --fingerprint
    dig TXT franco.net.eu.org +short | grep 'public-key-git-sig-fingerprint'
    

    If in doubt, contact me privately to arrange a key exchange.

  3. run the safe install script with Bash:

    ./safe_install.sh
    
  4. follow the post-installation instructions from the one minute setup

Documentation

https://docs.franco.net.eu.org/md-toc/

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

CLI Helps

$ md_toc --help
$ md_toc cmark --help
$ md_toc commonmarker --help
$ md_toc github --help
$ md_toc gitlab --help
$ md_toc goldmark --help
$ md_toc redcarpet --help

Extras

HTML output

If you use Pandoc you can generate an HTML output starting from a markdown file. You can do something like:

pandoc --from=commonmark --to=html -o a.html README.md

Contributing

See Contributing.

Consulting and custom integrations

If you need help or custom endpoints and integrations, I'm available for contract-based freelance consulting and custom Python development:

License

Copyright (C) 2017-2026 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 http://www.gnu.org/licenses/.

Git forge mirrors

URL Type Notes
https://github.com/frnmst/md-toc RW Official home
https://codeberg.org/frnmst/md-toc RW Mirror
https://framagit.org/frnmst/md-toc RW Mirror
https://repos.franco.net.eu.org/frnmst/md-toc RW Mirror

Support this project

Release files for md-toc 9.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for md-toc 9.0.1
File Size Uploaded
md_toc-9.0.1.tar.gz 94.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for md-toc 9.0.1
File Interpreter ABI Platform
md_toc-9.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 165.5 kB

Release files / md_toc-9.0.1.tar.gz

Download URL md_toc-9.0.1.tar.gz
Size 94.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d66b66228d4dcd58601b06e3a88a404623e9a0e90c1bece1e707cdf0e95aa28b
BLAKE2b-256 checksum
How to use checksums
d907559b9db91a7ee12ebdd71858caf372c0dfd3913a5cb93a14a4514716170f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / md_toc-9.0.1-py3-none-any.whl

Download URL md_toc-9.0.1-py3-none-any.whl
Size 71.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e23bb0fb4157e575d738fa38b1855d80318e59d426254b5810f5fa212c1cfcd
BLAKE2b-256 checksum
How to use checksums
0f1367efcea9747ffa58c475dba427b974697dc557f92c898adb7679c6dfe32b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

9.0.1 This release

2 release files

9.0.0

2 release files

8.2.3

2 release files

8.2.2

2 release files

8.2.1

2 release files

8.2.0

2 release files

8.1.9

2 release files

8.1.8

2 release files

8.1.7

2 release files

8.1.6

2 release files

8.1.5

2 release files

8.1.4

2 release files

8.1.3

2 release files

8.1.2

2 release files

8.1.1

2 release files

8.1.0

2 release files

8.0.1

2 release files

8.0.0

2 release files

7.2.0

2 release files

7.1.0

2 release files

7.0.5

2 release files

7.0.4

2 release files

7.0.3

2 release files

7.0.2

2 release files

7.0.1

2 release files

7.0.0

2 release files

6.0.2

2 release files

6.0.1

2 release files

6.0.0

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.0.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page