Skip to main content

markdown-toc-creator

This is a command-line tool to create table of contents from markdown files.


Table of Contents


1. Why should you use this tool?

Tables of contents (ToC) make markdown documents easier to understand and to navigate.

But generating them (and more importantly, keeping them updated) is tedious and error prone. This tool can automate this process for you, saving your time and energy for the truly important task: the document's content itself.

Additional, I've written another tool, markdown-heading-numbering to help you add numbering to markdown headers. You are welcome to try it out too. (More notes below.)

2. How to use this tool?

2.1. As a command-line tool

Install this tool:

pip install markdown-toc-creator

And then run this command:

markdown-toc-creator <PATH_TO_FOLDER_OR_FILE>

(Config options are introduced in the seciton below.)

By default, a table of contents will be generated below Line 1 of every markdown file in your path (or in your specified files).

If you want to manually control which markdown file will have ToC generated (and where in the file the ToC is), check out the --proactive config option below.

2.2. As a pre-commit hook

Add something like this into your .pre-commit-config.yaml file:

  - repo: https://github.com/jsh9/markdown-toc-creator
    rev: <REPLACE_WITH_LATEST_VERSION>
    hooks:
      - id: markdown-toc-creator
        args:
          - --proactive=False
          - --add-toc-title=True

(You can change the args yourself. Check out the following section for all possible args.)

3. Configuration options

3.1. --proactive (default: True)

3.1.1. If --proactive=True

If True (default), the tool creates a table of contents below Line 1 of every markdown file in your specified path.

A pair of HTML comments will be added to the file, and the table of contents are generated within it:

<!--TOC-->

<!--TOC-->

3.1.2. If --proactive=False

The tool would not be in the "proactive" mode.

In this mode, if you want tables of contents auto-generated for some markdown files, you'll need to put the HTML comment pair (double <!--TOC-->) into those files, and then a ToC will be generated between the comments.

3.2. --style (default: "github")

Pick between "gitlab" or "github" (default).

3.3. --exclude (default: '.git|.tox|.pytest_cache')

Regex of file/folder name patterns to exclude, default: '\.git|\.tox|\.pytest_cache'

3.4. --in-place (default: True)

Whether to actually add the table of contents into the original markdown file. Default: True.

3.5. --quiet (or -q ,default: False)

If True, the generated table of contents will not be printed to the terminal. Default: False.

3.6. --skip-first-n-lines (default: 1)

How many lines of the markdown file (starting from the top) to skip. This is useful because usually the initial line of markdown file is H1 (header level 1, with a #), and we don't want it to be included in the table of content.

3.7. --add-toc-title (default: True)

If True (default), insert a bolded title line after the opening placeholder. Combine with --toc-title to customise the text.

3.8. --toc-title (default: 'Table of Contents')

The text to use for the table-of-contents title when --add-toc-title is enabled. Defaults to "Table of Contents" and supports arbitrary Unicode strings (for example, emojis or non-English phrases).

This config option has no effect if --add-toc-title is False.

3.9. --add-horizontal-rules (default: True)

If True (default), wrap the generated entries between horizontal rules for additional separation (the ToC title, if added, will be inside the rules). Pair this with --horizontal-rule-style to pick which thematic break format to use.

3.10. --horizontal-rule-style (default: 'mdformat')

Controls the style of the horizontal rules inserted when --add-horizontal-rules is enabled:

  • mdformat (default): uses 70 underscores to match mdformat's style
  • prettier: uses --- to match Prettier's style

4. Compatibility with other formatters

4.1. With markdown-heading-numbering

If you are also using my other markdown formatter markdown-heading-numbering as a pre-commit hook to for create tables of contents in your markdown files, it's better to use that hook before this one.

4.2. With mdformat

This tool is fully compatible with mdformat as pre-commit hooks.

Metadata

Release files for markdown-toc-creator 0.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 markdown-toc-creator 0.1.3
File Size Uploaded Provenance Actions
markdown_toc_creator-0.1.3.tar.gz 18.6 kB Without provenance Details for markdown_toc_creator-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for markdown-toc-creator 0.1.3
File Interpreter ABI Platform Actions
markdown_toc_creator-0.1.3-py3-none-any.whl Python 3 none any Details for markdown_toc_creator-0.1.3-py3-none-any.whl

Total release size: 32.0 kB

Release files / markdown_toc_creator-0.1.3.tar.gz

Download URL markdown_toc_creator-0.1.3.tar.gz
Size 18.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8014e9e8c345f34ed0c953afd099742b742413001ffadec1f28455ffeed5cabe
BLAKE2b-256 checksum
How to use checksums
38b6c47a8e5b91f40f3abfc1ca1168e4aad06bc8421d06c52369efa6e4242a16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.24

Release files / markdown_toc_creator-0.1.3-py3-none-any.whl

Download URL markdown_toc_creator-0.1.3-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
046e93d89b4e765733ce43e4b003a9e7974e60b14bcba62ccccd9b1bdbb03abb
BLAKE2b-256 checksum
How to use checksums
e8e4ae8d8e9217e40c669167d0fda238b171d761b9c6d2f483940aa41c8a761a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.24

Security for markdown-toc-creator 0.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