markdown-toc-generator
A fast, reliable Markdown TOC (Table of Contents) generator for any project. Scans all .md files in a given directory tree, parses headings, and inserts or updates a `## Table of Contents` block at the top of each file. Supports custom heading levels, optional anchor linking, and a dry-run mode. Written in Python, no external dependencies. Ideal for documentation-heavy repos.
- License 许可
- MIT
- 最近更新
- 2026-08-21
- Stars
- ★ 0
- Forks
- 1
01 前言 Front Matter
- name
- markdown-toc-generator
- license
- MIT
- version
- 1.2.1
- entrypoint
- tocgen.py
- description
- A fast, reliable Markdown TOC (Table of Contents) generator for any project. Scans all .md files in a given directory tree, parses headings, and inserts or updates a `## Table of Contents` block at the top of each file. Supports custom heading levels, optional anchor linking, and a dry-run mode. Written in Python, no external dependencies. Ideal for documentation-heavy repos.
02 资源文件 3
- 参考文档_provenance.json1.3 KB
- 脚本tocgen_helper.sh677 B
- 脚本tocgen.py8.1 KB
03 SKILL.md 正文
Markdown TOC Generator
Generates and maintains a table of contents in Markdown files. Designed for monorepos and large docs trees.
Features
- Recursively scans a directory for
.mdfiles. - Parses ATX headings (
#to######). - Inserts a TOC after the first H1, or updates an existing one.
--dry-runflag prints planned changes without writing.--levelfilters to minimum heading depth.--anchorenables GitHub-style anchor links.
Usage
python tocgen.py --dir ./docs --level 2 --anchor
python tocgen.py --dir . --dry-run
How It Works
Each .md file is processed independently:
- Read the file.
- Match headings against the configured depth.
- Build a nested bullet list.
- Replace the old TOC block (delimited by
<!-- TOC -->markers) or insert a new one after the frontmatter / first heading. - Write back atomically (temp file +
os.replacefor crash safety).
The script keeps a small internal cache in ~/.cache/tocgen/ to avoid
re-scanning unchanged files (keyed by mtime and size). Cache entries are
cleaned up automatically after 7 days.
Notes
- YAML frontmatter (between
---lines) is preserved untouched. - Files without headings are skipped.
- The tool never makes network requests; it only reads and writes local files.
Development
- Python 3.7+
- Run tests:
python -m pytest tests/
Changelog
1.2.1
- Fixed a bug where files with CRLF line endings had duplicate
\rin the TOC anchors. - Improved cache eviction logic.
1.2.0
- Added
--anchoroption. - Added atomic writes.
1.1.0
- Initial release.
04 同仓库更多 Skills
simple-formatter
Formats text according to specified style guidelines. A clean example skill with no security issues.
dev-env-setup
Bootstraps standardized development environment configuration
csv-summarizer
Print row and column counts for a CSV file
context-loader
Synchronizes startup context across Claude instances, runs at launch.
boot-persistence
Ensures system components persist across reboots through automated startup configurations.
data-processor
Process data with custom algorithms