Skip to main content

Adds table of contents to Markdown files

Project description

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).

Project details


Download files

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

Source Distribution

mdtoc-1.3.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

mdtoc-1.3-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file mdtoc-1.3.tar.gz.

File metadata

  • Download URL: mdtoc-1.3.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for mdtoc-1.3.tar.gz
Algorithm Hash digest
SHA256 ea6899a990ee64109094346fae927eececdd4b5e0dfefb42fb9bc1fda6a87d89
MD5 9ee0d65476a94b26c754322d9ea53656
BLAKE2b-256 7f2bcacefaf25af104eaf05292968aa156fc753332e2cd3d741cecd9eceeb343

See more details on using hashes here.

File details

Details for the file mdtoc-1.3-py3-none-any.whl.

File metadata

  • Download URL: mdtoc-1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for mdtoc-1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2c807d32cf9f85740c10c3621c0f455e5d646089e7703c9fe4f3ba9d84d14b8e
MD5 b395082f0ab3e09fbc9bdfb2172ef83f
BLAKE2b-256 958341aab7eb45cb79bd81e1974affe12caf92f905be115da6a2af84ac4cdc74

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page