← 中文资源

简体中文版本

Markdown 目录生成器

提取 Markdown 的 ATX 标题(跳过围栏代码块),生成带 GitHub 风格锚点的嵌套目录列表。

阅读英文原文

目标

逐行扫描标题并记录层级,围栏代码块内的 # 不算标题;slugify 按 GitHub 规则小写、去标点、空格转连字符。

要点

渲染按层级缩进两空格;demo 对四个标题的 README 输出嵌套列表,锚点与 GitHub 完全一致。

提示

Setext 下划线式标题不被识别;重复锚点的消歧只做单后缀。

验证

node –test 验证围栏跳过与 slug 规则;CLI 对示例 README 输出目录。 复现命令:

node --test toc.test.mjs
node toc.mjs README.md

预期输出要点:For a README with three heading levels the CLI prints nested list items whose anchors match GitHub slug rules.

实测证据:Node 22.23.2: node –test -> 3 passed; CLI emitted a nested list with GitHub-style anchors for a four-heading README sample.