Skip to main content

Markdown TOC Generator

mdtoc is a command-line utility to generate table of contents within Markdown (.md) files.

Supports Python 2.7 | 3.4 | 3.5 | 3.6 | 3.7
Latest Release latest release
Package Status status
License license

Install

Install via pip install mdtoc.

Basic Usage

Add these delimiters to your Markdown file:

<!---toc start-->
<!---toc end-->

Then, from the command line, run:

$ mdtoc /path/to/myfile.md

This will overwrite the target file /path/to/myfile.md in-place with the table of contents replacing the text in between the delimiters marked above. (The delimiters themselves are invisible comments when rendered.) If you want to write to a new file, use --outfile. If you want to write just the TOC to stdout, use --stdout.

Technical Details

mdtoc parses Markdown "atx-style" headers only: 1-6 hash characters (#) at the start of the line followed by the header. It does not currently detect "setext-style" (underlined) headers.

The Daring Fireball page is the closest thing that exists to an original, canonical syntax specification for Markdown. (The page is created and hosted by John Gruber, the initial developer of Markdown as a language.) However, this page leaves a good amount of ambiguity. Because of that, mdtoc also incorporates rules from GitHub-flavored Markdown, which gives a fuller specification:

An ATX heading consists of a string of characters, parsed as inline content, between an opening sequence of 1–6 unescaped # characters and an optional closing sequence of any number of unescaped # characters. The opening sequence of # characters must be followed by a space or by the end of line. The optional closing sequence of #s must be preceded by a space and may be followed by spaces only. The opening # character may be indented 0-3 spaces. The raw contents of the heading are stripped of leading and trailing spaces before being parsed as inline content. The heading level is equal to the number of # characters in the opening sequence.

Please consider the GitHub-flavored Markdown rules to be the definitive empirical rulebook used by this tool. If you find a violation of that, pull requests are appreciated.

You can also check live examples in tests/examples.md. This doc serves as touch-and-feel proof of GitHub's take on Markdown formatting, as well including some novel examples that even GitHub's page itself does not cover.

One break from GitHub is that this tool counts tabs as spaces, for all intents and purposes. This is different from the GitHub specification, which defines a space strictly as U+0020.

Two other small notes:

  • mdtoc will ignore comments prefaced with # that occur in Markdown code blocks (```).
  • mdtoc does not check for congruency/continuity of header levels. If a level-3 header comes directly after a level-1 header, that is on you and will be rendered as-is.

Full Command-Line Help Doc

$ mdtoc --help
usage: mdtoc [-h] [--version] [--check-links] [--outfile OUTFILE | --stdout]
             markdown_file

Generates table of contents for Markdown files.

The algorithm searches for the text blocks
between the delimiters:

<!---toc start--->
... anything ...
<!---toc end--->

The contents of the block are then replaced
by a table of contents.

positional arguments:
  markdown_file      relative or abs. path of the Markdown
                     (.md) file to overwrite

optional arguments:
  -h, --help         show this help message and exit
  --version          show program's version number and exit
  --check-links      find all hyperlinks and ensure that
                     they point to something valid
  --outfile OUTFILE  instead of overwriting the input file,
                     write to this file instead
  --stdout           don't write or overwrite any file;
                     just print the TOC to stdout

Created by Scott Frazer (https://github.com/scottfrazer).

Metadata

Release files for mdtoc 1.3

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

Source distribution (sdist)

Source distribution for mdtoc 1.3
File Size Uploaded Provenance
mdtoc-1.3.tar.gz 7.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mdtoc 1.3
File Interpreter ABI Platform
mdtoc-1.3-py3-none-any.whl Python 3 none any Details

Total release size: 16.0 kB

Release files / mdtoc-1.3.tar.gz

Download URL mdtoc-1.3.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ea6899a990ee64109094346fae927eececdd4b5e0dfefb42fb9bc1fda6a87d89
BLAKE2b-256 checksum
How to use checksums
7f2bcacefaf25af104eaf05292968aa156fc753332e2cd3d741cecd9eceeb343
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release files / mdtoc-1.3-py3-none-any.whl

Download URL mdtoc-1.3-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c807d32cf9f85740c10c3621c0f455e5d646089e7703c9fe4f3ba9d84d14b8e
BLAKE2b-256 checksum
How to use checksums
958341aab7eb45cb79bd81e1974affe12caf92f905be115da6a2af84ac4cdc74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Security for mdtoc 1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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