markdown-it-table-of-contents
Advanced tools
Changelog
[0.9.0] - 2025-01-21
omitTag
can override the default tag <!-- omit from toc -->
Changelog
[0.8.0] - 2024-09-10
getTokensText
to override how text is extracted from tokens to build headlines and slugs (fixes #61), similar to the function in markdown-it-anchor.Changelog
[0.7.0] - 2024-09-09
Two new options that accept functions that return HTML to render custom containers (and more elements if necessary):
md.use(markdownItTOC, {
transformContainerOpen: () => {
return '<nav class="my-toc"><button>Toggle</button><h3>Table of Contents</h3>';
},
transformContainerClose: () => {
return '</nav>';
}
});
Input:
[[toc]]
Output before:
<p><div class="table-of-contents"></div></p>
Output now:
<div class="table-of-contents"></div>
The TOC now is generated in block mode, which removes the wrapping p
tag. Wrapping a div
in a p
is considered invalid HTML.
If you really need a wrapping p-element, you can emulate the old behavior with the new container override functions:
const md = new MarkdownIt();
md.use(markdownItTOC, {
transformContainerOpen: () => {
return '<p><div class="table-of-contents">';
},
transformContainerClose: () => {
return '</div></p>';
}
});
Be aware that the old tests/examples now behave differently when using soft breaks before the [[toc]] markup:
Input:
# Article
Text with soft line break (two spaces)
[[toc]]
## Headline
Output before:
<h1>Article</h1>
<p>Text with soft line break (two spaces)<br>
<div class="table-of-contents">...</div></p>
Output now:
<h1>Article</h1>
<p>Text with soft line break (two spaces)</p>
<div class="table-of-contents">...</div>
Changelog
[0.6.0] - 2021-11-12
The TOC generator was rewritten, because the old on-the-fly generator couldn't deal with unexpected order of headings and double-indentations. It is now a three-step process:
Although all tests pass, this release could introduce some breaking changes for you, if you relied on the old way of doing things. Check the test cases to get a better understanding how this plugin handles various cases.
markdown-it-attrs
(fixes #54)markdown-it-attrs
or markdown-it-anchor
)