Skip to main content

Generate a table of contents for a Markdown file.

Project description

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.
  • Run from the directory tree that owns the target file. Files outside the working directory are rejected.
  • Symlinks are refused.
  • 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>

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

toc_markdown-0.1.0.tar.gz (80.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

toc_markdown-0.1.0-py3-none-any.whl (31.7 kB view details)

Uploaded Python 3

File details

Details for the file toc_markdown-0.1.0.tar.gz.

File metadata

  • Download URL: toc_markdown-0.1.0.tar.gz
  • Upload date:
  • Size: 80.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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}

File hashes

Hashes for toc_markdown-0.1.0.tar.gz
Algorithm Hash digest
SHA256 efe0e21e73186c95357deeef27cb3dfca32f05c5ed82d398beb6a727af5c6738
MD5 51403b6f54cda881d88bcb815778ca5f
BLAKE2b-256 83d4f4fef64a76a3d252fa3940fe11923e5ffb9bc3bed4a79dfb1ef572624fef

See more details on using hashes here.

File details

Details for the file toc_markdown-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: toc_markdown-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 31.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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}

File hashes

Hashes for toc_markdown-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c86aa9fa16354ecb97550a0b7b65f42505275ee313387a506d3ae83919349ef6
MD5 c41157b08c28ad57f9b7cc010ae5bacc
BLAKE2b-256 8ba21e4c6b0f05a0bfe051eaa397a9bb4d45048806be21fe0dba3708aeb76d94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page