npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cs-magic/md-toc

v0.5.2

Published

A flexible markdown table of contents generator with configurable styles

Downloads

9

Readme

md-toc

md-toc为什么选择 md-toc?安装快速开始进阶使用配置选项常见问题贡献指南License致谢

一个强大的 Markdown 目录生成器,基于 md-toc 并提供了显著增强,包括完整的中文支持、多种目录样式和丰富的自定义选项。

为什么选择 md-toc?

  • 🀄️ 完整的中文支持 - 完美支持中文标题和链接
  • 🎯 多样化的目录样式 - 支持横向(适合简短目录)和纵向(适合详细目录)两种风格
  • ⚡️ 批量处理能力 - 轻松处理多个文档,支持 glob 模式匹配
  • 🔄 实时监听 - 支持文件变化自动更新,完美集成到你的写作流程
  • 🎨 高度可定制 - 灵活的配置选项,包括目录深度、样式和标记
  • 📦 多种使用方式 - 支持命令行、配置文件和 API 调用,满足不同场景需求
  • 🚀 专业的日志系统 - 提供详细的调试信息,让问题排查更轻松

安装

npm install -g @cs-magic/md-toc
# 或
yarn global add @cs-magic/md-toc
# 或
pnpm add -g @cs-magic/md-toc

快速开始

  1. 在你的 Markdown 文件中添加目录标记:
<!-- toc -->
<!-- tocstop -->
  1. 运行命令生成目录:
md-toc README.md

就是这么简单!

进阶使用

命令行选项

# 自动插入目录标记(如果文件中没有)
md-toc README.md --auto-insert

# 使用横向样式(适合简短目录)
md-toc README.md --style horizontal

# 设置最大标题层级
md-toc README.md --max-level 3

# 监听文件变化
md-toc README.md --watch

# 设置日志级别(debug, info, warn, error)
md-toc README.md --log-level debug

# 处理多个文件
md-toc doc1.md doc2.md

# 使用 glob 模式
md-toc "docs/*.md"

配置文件

创建 .tocrc.json.tocrc.js 文件:

{
  "files": ["README.md", "docs/*.md"],
  "style": "horizontal",
  "autoInsert": true,
  "maxLevel": 2,
  "watch": false,
  "logLevel": "info"
}

API 使用

import { generateToc } from "md-toc";

// 生成目录
await generateToc("README.md", {
  style: "horizontal",
  autoInsert: true,
  maxLevel: 2,
});

// 批量处理
await generateToc(["doc1.md", "doc2.md"], {
  style: "vertical",
});

// 使用 glob 模式
await generateToc("docs/*.md", {
  watch: true,
});

配置选项

| 选项 | 说明 | 默认值 | 描述 | | ------------ | ---------------- | ---------- | ------------------------------------------ | | style | 目录样式 | vertical | horizontal(横向)或 vertical(纵向) | | autoInsert | 自动插入目录标记 | false | 当文件中没有目录标记时自动插入 | | minLevel | 最小标题层级 | 2 | 控制目录中包含的标题层级 | | maxLevel | 最大标题层级 | 2 | 控制目录中包含的标题层级 | | watch | 监听文件变化 | false | 当文件变化时自动更新目录 | | logLevel | 日志级别 | info | error/warn/info/debug 控制日志输出 | | markers | 自定义目录标记 | 见下文 | 自定义目录的开始和结束标记 |

目录标记

默认的目录标记:

<!-- toc -->

目录将在这里生成

<!-- tocstop -->

也支持单行格式:

<!-- toc --><!-- tocstop -->

如果你想在文档中展示目录标记而不让它被处理,需要在标记前添加反斜杠:

\<!-- toc -->

常见问题

为什么选择 md-toc 而不是 markdown-toc?

md-toc 是基于 markdown-toc 开发的增强版本,主要解决了以下问题:

  1. 中文支持 - markdown-toc 在处理中文标题时存在问题,而 md-toc 完美支持中文
  2. 更多功能 - 提供了更丰富的功能,如多种目录样式、文件监听、详细的日志等
  3. 更好的开发体验 - 提供了更友好的 API 和配置选项,支持 TypeScript

目录链接不起作用?

确保你的标题符合 Markdown 规范,不包含特殊字符。如果仍有问题,可以尝试使用 --log-level debug 查看详细日志。

贡献指南

欢迎提交 Issue 和 Pull Request!在贡献代码前,请先阅读我们的贡献指南

License

MIT

致谢