Skip to main content

Build Status PyPI version

mdformat-toc

Mdformat plugin to generate a table of contents

Table of Contents generated with mdformat-toc

Description

Mdformat-toc is an mdformat plugin that adds mdformat the capability to auto-generate a table of contents (ToC). The ToC is generated in a user-specified location in the Markdown file.

Mdformat-toc, by default, creates an HTML anchor for each heading listed in the ToC. ToC links should therefore be compatible with any well-behaved Markdown renderer (including GitLab's renderer).

HTML anchor generation can be disabled, in which case a user should configure a slug function that is compatible with the Markdown renderer used (GitHub and GitLab slug functions are currently supported).

Install

pip install mdformat-toc

Usage

Add the following line to your Markdown file. A ToC will be generated in the location indicated by it.

<!-- mdformat-toc start -->

After adding the indicator line, simply run

mdformat <filename>

and mdformat will generate a ToC.

Configuration

Arguments can be added to the indicator line to alter how the ToC is generated. An indicator line with the default options would look like:

<!-- mdformat-toc start --slug=github --maxlevel=6 --minlevel=1 -->

Placing more than one indicator lines in a document is currently not supported.

Minimum and maximum heading levels

A user can configure a range of heading levels to be included in the ToC (and to be "anchored"). For instance, the following configuration will only list 2nd, 3rd and 4th level headings in the ToC:

<!-- mdformat-toc start --minlevel=2 --maxlevel=4 -->

Disabling anchor generation

By default, an HTML anchor is appended to each heading. For instance, the following heading

# Some title

might be formatted as

# Some title<a name="some-title"></a>

This ensures that ToC links do not rely on a Markdown renderer to create HTML anchors, and makes the links universally compatible.

ToC links are by default compatible with the anchors generated by GitHub's Markdown renderer. If your Markdown is only hosted on GitHub, you can disable mdformat-toc's HTML anchor generation:

<!-- mdformat-toc start --no-anchors -->

Changing the slug function

Mdformat-toc defaults to using GitHub's slug function.

If your Markdown is not hosted on GitHub you may want to use GitLab's slug function instead:

<!-- mdformat-toc start --slug=gitlab --no-anchors -->

NOTE: Unlike GitLab, GitHub requires using its own slug function in order for ToC links to work expectedly. Creating HTML anchors and using a non-GitHub slug function is not GitHub compatible because GitHub's Markdown renderer modifies the HTML anchors mdformat-toc creates. The default configuration (GitHub slug function and anchor generation) is the only configuration cross-compatible with GitHub and GitLab.

Metadata

Release files for mdformat-toc 0.5.0

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

Source distribution (sdist)

Source distribution for mdformat-toc 0.5.0
File Size Uploaded
mdformat_toc-0.5.0.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mdformat-toc 0.5.0
File Interpreter ABI Platform
mdformat_toc-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.6 kB

Release files / mdformat_toc-0.5.0.tar.gz

Download URL mdformat_toc-0.5.0.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
28e87519fe58d5f25133ba27998bfcba46f5710a40df9e4a19da787e03088d52
BLAKE2b-256 checksum
How to use checksums
e864023dac7c53aa2c915838a4ce52fbb724db41635b559024a943cb2f036586
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / mdformat_toc-0.5.0-py3-none-any.whl

Download URL mdformat_toc-0.5.0-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d26e222db39e3fe7527644cf3d99f83cc8f66295156855e46b054bc4636b328
BLAKE2b-256 checksum
How to use checksums
71c98bce9c6a97c4968d0381d09ae8b8e5a9d8dea292896f37c898fe87da4a71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

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