开源软件名称(OpenSource Name):cmaas/markdown-it-table-of-contents开源软件地址(OpenSource Url):https://github.com/cmaas/markdown-it-table-of-contents开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):markdown-it-table-of-contentsA table of contents plugin for Markdown-it. Simple, customizable and with a default slugifier that matches that of https://www.npmjs.com/package/markdown-it-anchor (>5.0.0). Usagevar MarkdownIt = require("markdown-it");
var md = new MarkdownIt();
md.use(require("markdown-it-anchor").default); // Optional, but makes sense as you really want to link to something, see info about recommended plugins below
md.use(require("markdown-it-table-of-contents")); Then add Example markdownThis markdown: # Heading
[[toc]]
## Sub heading 1
Some nice text
## Sub heading 2
Some even nicer text ... would render this HTML using the default options specified in "usage" above: <h1 id="heading">Heading</h1>
<div class="table-of-contents">
<ul>
<li><a href="#heading">Heading</a>
<ul>
<li><a href="#sub-heading-1">Sub heading 1</a></li>
<li><a href="#sub-heading-2">Sub heading 2</a></li>
</ul>
</li>
</ul>
</div>
<h2 id="sub-heading-1">Sub heading 1</h2>
<p>Some nice text</p>
<h2 id="sub-heading-2">Sub heading 2</h2>
<p>Some even nicer text</p> OptionsYou may specify options when md.use(require("markdown-it-table-of-contents"), options); These options are available:
By default, TOC headings will be formatted using markdown-it's internal MD formatting rules (i.e. it will be formatted using the same rules / extensions as other markdown in your document). You can override this behavior by specifying a custom
function format(content, md) {
// manipulate the headings as you like here.
return manipulatedHeadingString;
}
function transformLink(link) {
// transform the link as you like here.
return transformedLink;
} Recommended pluginsBy default, markdown-it-table-of-contents collects all headings and renders a nested list. It uses the markdown-it-anchorThis plugin transforms all headlines in a markdown document so that the HTML code includes an id. It slugifies the headline: ## Hello world, I think you should read this article Becomes <h2 id="hello-world-i-think-you-should-read-this-article">Hello world</h2> markdown-it-attrsThis plugin lets you attach custom attributes to your headlines. This is especially useful, if you have long headlines but want short anchors: ## Hello world, I think you should read this article {#hello} Becomes <h2 id="hello">Hello world, I think you should read this article</h2> Full example with unusual headline orderOf course, both plugins can be combined. markdown-it-anchor ignores headlines that already have an id attribute. Furthermore, markdown-it-table-of-contents can handle unusual heading orders. Consider the full example below: var md = new MarkdownIt();
md.use(markdownItTOC, {
"includeLevel": [2,3,4]
});
md.use(require("markdown-it-attrs"));
md.use(require("markdown-it-anchor")); # Article
[[toc]]
### A message from our sponsors
Ad
## Hello world, I think you should read this article {#hello}
Lorem ipsum
## What's next?
Read this next...
#### See related articles {#related} HTML output: <h1 id="article">Article</h1>
<p>
<div class="table-of-contents">
<ul>
<li>
<ul>
<li><a href="#a-message-from-our-sponsors">A message from our sponsors</a></li>
</ul>
</li>
<li><a href="#hello">Hello world, I think you should read this article</a></li>
<li><a href="#what's-next%3F">What's next?</a>
<ul>
<li>
<ul>
<li><a href="#related">See related articles</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</p>
<h3 id="a-message-from-our-sponsors">A message from our sponsors</h3>
<p>Ad</p>
<h2 id="hello">Hello world, I think you should read this article</h2>
<p>Lorem ipsum</p>
<h2 id="what's-next%3F">What's next?</h2>
<p>Read this next...</p>
<h4 id="related">See related articles</h4> Additional infos
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论