Skip to main content

Table of Contents generated with DocToc

DocToc

License PyPI version

DocToc is a command-line tool that automatically generates and updates a table of contents (TOC) for Markdown files. It scans your Markdown, identifies headers, and inserts a TOC with clickable links — skipping headers inside fenced code blocks.

Prerequisites

  • Python 3.8+
  • pip

Installation

pip install doctoc

Or from source:

git clone https://github.com/ktechhub/doctoc.git
cd doctoc
pip install .

Usage

doctoc [OPTIONS] MARKDOWN_FILES...

MARKDOWN_FILES accepts one or more file paths or glob patterns:

doctoc README.md
doctoc docs/README.md CHANGELOG.md
doctoc '**/*.md'

On the first run, DocToc inserts the TOC at the top of the file. On subsequent runs it updates the existing TOC in place.

Options

Option Short Description
--outfile PATH -o Write output to a separate file instead of overwriting the input (single-file only).
--check-links -cl Validate all hyperlinks found in the file.
--title TEXT -t Custom title line for the TOC block (default: bold "Table of Contents" with attribution).
--max-depth INT -d Limit TOC to headings up to this depth (e.g. 2 includes only H1 and H2).
--check -c Check whether the TOC is up to date without writing; exit non-zero if any file is stale.
--exclude TEXT -e Exclude headers whose text contains this value from the TOC (repeatable for multiple exclusions).
--help Show help and exit.

Examples

Basic — generate or update TOC:

doctoc README.md
# Success: wrote TOC to README.md

Custom output file:

doctoc README.md -o README_with_toc.md
# Success: wrote TOC to README_with_toc.md

Limit TOC depth to H1 and H2 only:

doctoc README.md --max-depth 2

Custom TOC title:

doctoc README.md --title "## Contents"

Check hyperlinks after generating TOC:

doctoc README.md --check-links
# Success: wrote TOC to README.md
# Checking hyperlinks in README.md...
# VALID: [DocToc](#doctoc)
# VALID: [Installation](#installation)
# VALID: [GitHub repository](https://github.com/ktechhub/doctoc)
# INVALID: [Broken](#missing-section)

Exclude specific headers from the TOC:

doctoc README.md --exclude "Appendix"

Process all Markdown files recursively:

doctoc '**/*.md'

CI check — fail if a TOC is stale, without writing:

doctoc --check '**/*.md'
# OK: README.md is up to date
# STALE: docs/CONTRIBUTING.md is out of date

Features

  • Generates and updates a TOC based on Markdown headers.
  • Skips headers inside fenced code blocks (``` and ~~~).
  • Supports multiple files and glob patterns in a single invocation.
  • --max-depth to limit which heading levels appear in the TOC.
  • --exclude to omit specific headers from the TOC by matching text (repeatable).
  • --title to customise the TOC heading line.
  • Link checker validates both internal anchor links and external HTTP(S) URLs, with a timeout and clear error output.
  • Writes to an optional output file instead of overwriting the source.
  • --check for CI: reports stale TOCs with a non-zero exit code, without modifying any file.

GitHub

For more details, visit the GitHub repository.

License

This project is licensed under the MIT License — see the LICENSE file for details.

Contribution

If you want to contribute, kindly see contribution.

Metadata

Release files for doctoc 1.4.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 doctoc 1.4.0
File Size Uploaded Provenance Actions
doctoc-1.4.0.tar.gz 11.9 kB Without provenance Details for doctoc-1.4.0.tar.gz

Built distribution (wheel)

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

Total release size: 21.0 kB

Release files / doctoc-1.4.0.tar.gz

Download URL doctoc-1.4.0.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
52a263202928ddd6df66610c09b82a148eb9f4e76ce991247afe8b5696f73ca1
BLAKE2b-256 checksum
How to use checksums
f2fd7940d52aae5f858ed75e147c8ebd85c6cd15c747893832b59d7f75fb03d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / doctoc-1.4.0-py3-none-any.whl

Download URL doctoc-1.4.0-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b168f625b897ee4fb88c59a6542a8159a0a366b1bef75f61606904fadd47eb82
BLAKE2b-256 checksum
How to use checksums
21aed6cb44c6d5874eb6c14f0e5b2da05f8db219f6a552743964097a72767735
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for doctoc 1.4.0

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