Docs TOC Generator - 文档总目录生成器
Purpose
Scan a docs directory (e.g., docs/tech/), read all _category_.json and markdown frontmatter, then generate a structured table-of-contents page that links to every document in the section.
When to Use
- •When the user mentions generating, updating, or refreshing a table of contents / index page
- •When the user says "生成总目录", "更新目录", "刷新目录索引" or similar phrases
- •When the user references an index file like
0-learn.mdand asks to populate it - •When the user adds new documents and wants the TOC updated
Workflow
Step 1: Identify Target
Determine:
- •Scan directory: The docs section to scan (e.g.,
docs/tech/). Default to the parent directory of the target file. - •Target file: The index/TOC file to update (e.g.,
docs/tech/0-learn.md).
Step 2: Scan Directory Structure
Recursively scan the target directory:
- •Read
_category_.jsonfiles to get:- •
label: Display name of the category - •
position: Sort order among sibling categories
- •
- •Read markdown/mdx file frontmatter to get:
- •
title: Document title - •
sidebar_position: Sort order within the category
- •
- •Skip image files, source code files, and the TOC file itself.
- •Note subdirectories as nested categories.
Step 3: Build TOC Structure
Organize the data into a tree:
code
Section (from _category_.json label, sorted by position)
├── Doc 1 (sorted by sidebar_position)
├── Doc 2
└── Subsection (nested _category_.json)
├── Doc 3
└── Doc 4
Sort rules:
- •Categories sorted by
positionfield in_category_.json - •Documents sorted by
sidebar_positionin frontmatter - •Documents without
sidebar_positionappear at the end - •Use the
titlefrom frontmatter as display text - •For documents without frontmatter title, derive from filename
Step 4: Generate Markdown Content
Generate the TOC content in this format:
markdown
--- id: index title: <Section Title> sidebar_position: 0 --- ## 总目录 ### <Category 1 Label> - [Doc Title 1](./relative/path/to/doc1) - [Doc Title 2](./relative/path/to/doc2) ### <Category 2 Label> - [Doc Title 3](./relative/path/to/doc3) - [Subdoc Title](./relative/path/to/subdoc)
Link format rules:
- •Use relative paths from the TOC file location
- •For
index.mdfiles, link to the directory (e.g.,./go/instead of./go/index.md) - •For
readme.mdfiles, treat them the same asindex.md— link to the directory (e.g.,./devtools/fix/instead of./devtools/fix/readme), because Docusaurus treatsreadme.mdas a directory index page - •For filenames with numeric prefixes used for sorting (e.g.,
0-openwrt.md,1-intro.md), the link path must omit the numeric prefix (e.g.,./network/openwrtinstead of./network/0-openwrt), because Docusaurus stripsnumberPrefixfrom the generated route by default - •Exception: The TOC file itself (e.g.,
0-learn.md) is excluded from scanning, so this rule does not apply to it - •For
.mdxfiles, include the extension in the path - •Nested categories use indented sub-lists
Step 5: Preserve Custom Content
If the target file already has custom content sections (like "技术文章" with external links):
- •Keep those sections intact at the bottom of the file
- •Only regenerate the "总目录" section
- •Identify custom sections as any
## headingthat is NOT "总目录"
Step 6: Write the File
Update the target file with the generated content, preserving:
- •Original frontmatter (if any)
- •Custom content sections appended after the generated TOC
Important Notes
- •Always sort categories and documents by their position/sidebar_position values
- •Use Chinese display names from
_category_.jsonlabel fields when available - •Generate relative links that work with Docusaurus routing
- •Docusaurus strips numeric prefixes (e.g.,
0-,1-) from filenames when generating routes. Links must use the path without the numeric prefix (e.g.,./network/openwrtnot./network/0-openwrt) - •
readme.mdis treated as a directory index by Docusaurus, so link to the directory path (e.g.,./devtools/fix/) instead of./devtools/fix/readme - •The skill works for any docs section (tech, life, essay), not just tech
- •If a category has an
index.md, link the category heading to it