Skip to main content

Markdown Table of Contents Generator

Generates a table of contents (TOC) for Markdown files. Detects headers and creates a linked TOC. Updates existing TOCs in-place when markers are present; otherwise prints to stdout.

toc_markdown

Table of Contents

  1. Quick Start
  2. Features
  3. Installation
  4. Usage
  5. Configuration
  6. Integration with Vim

Quick Start

Add TOC markers to your Markdown file:

<!-- TOC -->
<!-- /TOC -->

Run:

toc-markdown README.md

The TOC appears between the markers. Run again to update.

Without markers, the TOC prints to stdout for manual insertion.

Features

  • Generates a table of contents from Markdown headers.
  • Updates existing TOCs between markers or prints to stdout.
  • Supports headings from levels 2 to 3 by default (configurable).
  • Provides clickable links to sections.
  • Preserves file structure and formatting.

Installation

Requirements: Python 3.11+

Using uv (recommended):

uv tool install toc-markdown

Using pip:

pip install toc-markdown

Usage

Run toc-markdown on a .md or .markdown file:

# Update file in-place (requires TOC markers)
toc-markdown path/to/file.md

# Print TOC to stdout (no markers in file)
toc-markdown path/to/file.md

# Customize header levels
toc-markdown README.md --min-level 1 --max-level 4

# Change list style
toc-markdown README.md --list-style "*"
toc-markdown README.md --list-style "-"

# Custom header text
toc-markdown README.md --header-text "## Contents"

# Preserve Unicode in slugs
toc-markdown README.md --preserve-unicode

# Custom indentation
toc-markdown README.md --indent-chars "  "

# Custom markers
toc-markdown README.md --start-marker "<!-- BEGIN TOC -->" --end-marker "<!-- END TOC -->"

Safety Limits

  • Only regular Markdown files (.md, .markdown) are accepted.
  • Files larger than 10 MiB are rejected. Increase via max_file_size in config or TOC_MARKDOWN_MAX_FILE_SIZE environment variable (up to 100 MiB).
  • Lines longer than 10,000 characters are rejected. Increase via max_line_length in config or TOC_MARKDOWN_MAX_LINE_LENGTH environment variable.
  • Files with more than 10,000 headers are rejected. Increase via max_headers in config.
  • Files must be valid UTF-8.
  • Updates use atomic writes via temporary files.

Run toc-markdown --help for all options.

Configuration

Create .toc-markdown.toml in your project root:

[toc-markdown]
min_level = 2
max_level = 3
list_style = "1."

Options

Option Default Description
start_marker <!-- TOC --> Opening marker
end_marker <!-- /TOC --> Closing marker
header_text ## Table of Contents TOC heading
min_level 2 Minimum header level to include
max_level 3 Maximum header level to include
list_style 1. Bullet style: 1., *, -, ordered, unordered
indent_chars (4 spaces) Indentation for nested entries
indent_spaces null Alternative to indent_chars; sets spaces count
preserve_unicode false Keep Unicode in slugs
max_file_size 10485760 (10 MiB) Maximum file size in bytes
max_line_length 10000 Maximum line length
max_headers 10000 Maximum headers to process

CLI flags override config file values.

Integration with Vim

Example mapping (for files with TOC markers):

autocmd FileType markdown nnoremap <buffer> <leader>t :w<cr>:silent !toc-markdown %:p<cr>:e<cr>

Press <leader>t in normal mode to save, update the TOC, and reload the buffer.

For files without markers (insert TOC at cursor):

autocmd FileType markdown nnoremap <buffer> <leader>T :r !toc-markdown %:p<cr>

Metadata

Release files for toc-markdown 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 toc-markdown 0.1.3
File Size Uploaded
toc_markdown-0.1.3.tar.gz 83.4 kB Details

Built distribution (wheel)

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

Total release size: 114.5 kB

Release files / toc_markdown-0.1.3.tar.gz

Download URL toc_markdown-0.1.3.tar.gz
Size 83.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8f77d0015d7322c7c6a83af273a9eafde534af2667da77b8e206c5e497ffe314
BLAKE2b-256 checksum
How to use checksums
05a0b05e8ff68711bab91a61cd84e07731d42fcaa07d49023f81b3a180ca54e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

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

Download URL toc_markdown-0.1.3-py3-none-any.whl
Size 31.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c46ad15ada444986b32a851cd97b62ba75500281e9577c08b572f011a1f1db5d
BLAKE2b-256 checksum
How to use checksums
58a1b424db6a9f057b2711b827f9de82565e67ff7d4d15a06f2ca483f5055565
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.2

2 release files

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