CLI tool that inserts and updates tables of contents in Markdown files in a git repo, with anchors compatible with GitHub and other hosts.
Overview
DocToc generates tables of contents for Markdown files in a local git repository. It scans files or folders recursively, processes .md, .markdown, and .mdx files, and writes TOC blocks with links that match anchors produced by GitHub or other sites when configured with the appropriate renderer mode.
You can run it on a single file, a list of paths, or an entire directory tree, skipping .git and node_modules by default. Existing DocToc sections are found via pragma comments and updated in place, and you can pin TOC placement with an in-file marker or choose automatic placement at the top or before the first heading.
Configuration covers heading level limits, TOC title and list formatting, indentation, minimum document length, dry-run mode for CI, stdout output for one file, and update-only mode that touches files that already have a TOC. Bitbucket, GitLab, and similar modes adjust link compatibility.
Installation is via npm global install. It can run as a pre-commit hook to keep TOCs current, and an unofficial Docker image exists for containerized CI use.
Key features
- Recursive Markdown TOC insertion
- GitHub-compatible anchor modes
- In-place TOC update via pragmas
- Configurable heading minlevel and maxlevel
- Dry-run for CI freshness checks
- Custom TOC title and list styles
- Pre-commit hook integration
- Ignore and exclude heading markers
Use cases
- Keeping README TOCs up to date
- CI checks that docs TOC is current
- Bulk TOC generation in a repo
- Git pre-commit TOC refresh