目录 

链接HTML标头并生成TOC。
叉形的 node-toc
用TypeScript重写并重构以减小包的大小(不使用整个lodash库)
安装
$ yarn add @shelf/table-of-contents
使用
const toc = require('@shelf/table-of-contents');
toc.untag
从字符串中剥离HTML标记。
const stripped = toc.untag(html);
toc.anchor
将文本字符串转换为url友好且不太难看的内容。
const anchor = toc.anchor(arbitraryText);
toc.unique
获取唯一名称并将返回的名称存储在名称中,以备将来使用惟一名称获取。
const names = {};
const guaranteedUniqueAnchor1 = toc.unique(names, toc.anchor(arbitraryText));
const guaranteedUniqueAnchor2 = toc.unique(names, toc.anchor(arbitraryText));
toc.process
锚定所有标头并内联生成的TOC,返回处理过的HTML。
const htmlWithAnchorsAndTOC = toc.process(html [, options]);
选项
- 占位符 -
RegExp-用于匹配TOC占位符。默认为/<!--\s*toc\s*-->/gi. - 因为这个方法调用
toc.anchorize和toc.toc方法,也可以指定它们的选项。
toc.anchorize
解析HTML,返回头部对象和锚定HTML的数组。
const obj = toc.anchorize(html [, options]);
选项
- 头 -
RegExp-用于匹配HTML标头。默认为/<h(\d)(\s*[^>]*)>([\s\S]+?)<\/h\1>/gi. - tocMin -
Number-添加到TOC的最小header级别。默认为2. - tocMax -
Number-最大头级别添加到TOC。默认为6. - anchorMin -
Number-要锚定的最小标头级别。默认为2. - anchorMax -
Number-最大头级别锚定。默认为6. - 头 -
String|Function- Lodash模板字符串或函数,用于锚定标头。
toc.toc
从标题对象数组生成TOC HTML。
const obj = toc.toc(headers [, options]);
选项
- openUL -
String|Function—Lodash模板用于生成TOC的字符串或函数。 - closeUL -
String|Function—Lodash模板用于生成TOC的字符串或函数。 - openLI -
String|Function—Lodash模板用于生成TOC的字符串或函数。 - closeLI -
String|Function—Lodash模板用于生成TOC的字符串或函数。 - TOC -
String|Function- Lodash模板字符串或函数,用于包装生成的TOC。
示例
默认的HTML非常棒,但是您可以自定义生成的HTML,例如:
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>',
});
发布
$ git checkout master
$ yarn version
$ yarn publish
$ git push origin master --tags
许可证
麻省理工学院© 架子上