@shelf/table-of-contents

中文文档
1.4.6发布于 2026/10/09UNLICENSEDTypeScriptES Module

Fork of node-toc to reduce bundle size & rewritten in TypeScript

原始使用文档
文档目录11 节
  1. Install
  2. Usage
  3. toc.untag
  4. toc.anchor
  5. toc.unique
  6. toc.process
  7. toc.anchorize
  8. toc.toc
  9. Examples
  10. Publish
  11. License

table-of-contents CircleCI

Linkify HTML headers and generate a TOC.

Forked from node-toc

Rewritten in TypeScript & refactored to reduce bundle size (by not using entire lodash library)

Install

$ yarn add @shelf/table-of-contents

Usage

const toc = require('@shelf/table-of-contents');

toc.untag

Strip HTML tags from a string.

const stripped = toc.untag(html);

toc.anchor

Convert a string of text into something URL-friendly and not too ugly.

const anchor = toc.anchor(arbitraryText);

toc.unique

Get a unique name and store the returned name in names for future unique-name-gettingness.

const names = {};
const guaranteedUniqueAnchor1 = toc.unique(names, toc.anchor(arbitraryText));
const guaranteedUniqueAnchor2 = toc.unique(names, toc.anchor(arbitraryText));

toc.process

Anchorize all headers and inline a generated TOC, returning processed HTML.

const htmlWithAnchorsAndTOC = toc.process(html [, options]);

options

  • placeholder - RegExp - Used to match TOC placeholder. Defaults to /<!--\s*toc\s*-->/gi.
  • Because this method calls the toc.anchorize and toc.toc methods internally, their options may be specified as well.

toc.anchorize

Parse HTML, returning an array of header objects and anchorized HTML.

const obj = toc.anchorize(html [, options]);

options

  • headers - RegExp - Used to match HTML headers. Defaults to /<h(\d)(\s*[^>]*)>([\s\S]+?)<\/h\1>/gi.
  • tocMin - Number - Min header level to add to TOC. Defaults to 2.
  • tocMax - Number - Max header level to add to TOC. Defaults to 6.
  • anchorMin - Number - Min header level to anchorize. Defaults to 2.
  • anchorMax - Number - Max header level to anchorize. Defaults to 6.
  • header - String | Function - Lodash template string or function used to anchorize a header.

toc.toc

Generate TOC HTML from an array of header objects.

const obj = toc.toc(headers [, options]);

options

  • openUL - String | Function - Lodash template string or function used to generate the TOC.
  • closeUL - String | Function - Lodash template string or function used to generate the TOC.
  • openLI - String | Function - Lodash template string or function used to generate the TOC.
  • closeLI - String | Function - Lodash template string or function used to generate the TOC.
  • TOC - String | Function - Lodash template string or function used to wrap the generated TOC.

Examples

The default HTML is pretty awesome, but you can customize the hell out of the generated HTML, eg.

const processedHTML = toc.process(unprocessedHTML, {
  header: '<h<%= level %><%= attrs %> id="<%= anchor %>"><%= header %></h<%= level %>>',
  TOC: '<div class="toc"><%= toc %></div>',
  openUL: '<ul data-depth="<%= depth %>">',
  closeUL: '</ul>',
  openLI: '<li data-level="H<%= level %>"><a href="#<%= anchor %>"><%= text %></a>',
  closeLI: '</li>',
});

Publish

$ git checkout master
$ yarn version
$ yarn publish
$ git push origin master --tags

License

MIT © Shelf

版本与发布记录

@shelf/table-of-contents 共发布 23 个版本,首次发布于 2021/11/05;过去 12 个月发布了 11 个版本。

版本发布日期距今
1.4.6latest2026/10/0925 天前
1.4.52026/09/022 个月前
1.4.42026/09/022 个月前
1.4.32026/08/232 个月前
1.4.22026/08/112 个月前
1.4.12026/08/082 个月前

关于 @shelf/table-of-contents 的常见问题

@shelf/table-of-contents 是什么?

node-toc的分支,以减少bundle的大小,并在TypeScript中重写

如何安装 @shelf/table-of-contents?

在项目目录运行 npm install @shelf/table-of-contents,或使用 pnpm add / yarn add / bun add。

@shelf/table-of-contents 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@shelf/table-of-contents 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@shelf/table-of-contents 可以商用吗?

@shelf/table-of-contents 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@shelf/table-of-contents 还在维护吗?

最新版本 1.4.6 发布于 2026/10/09(25 天前);至今共发布 23 个版本,其中过去 12 个月发布了 11 个;首次发布于 2021/11/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。