Generate table of contents for Markdown files
Project description
Table of Contents generated with DocToc
DocToc
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). |
--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)
Process all Markdown files recursively:
doctoc '**/*.md'
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-depthto limit which heading levels appear in the TOC.--titleto 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.
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file doctoc-1.1.1.tar.gz.
File metadata
- Download URL: doctoc-1.1.1.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d390d4a44e93b432844a71390bcf322fcd9fd80cb5c9bc4fdfcada0e081ea42f
|
|
| MD5 |
b504e46b7b527f4d59b4fe28a583687e
|
|
| BLAKE2b-256 |
33794bef2d15b90c594906ba99ebf364ed4c2abd85ddfe3256721948433cdc7c
|
File details
Details for the file doctoc-1.1.1-py3-none-any.whl.
File metadata
- Download URL: doctoc-1.1.1-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ace59b11ebf970b89b12913fdda6cdcdd824cc27a5f6ed61a65723ab7ca7aac6
|
|
| MD5 |
767d5cb985a68cb38163dc6789a62f0b
|
|
| BLAKE2b-256 |
4edb4e551bc9fc97e6ef6f8b0d2980d6fcf278d055bc537dd7dedb8421a5bee4
|