Generate a table of contents with markdown-it plugins
How markdown-it TOC plugins build a table of contents, what they change in your output, and when a plain, hand-written list of links is the better choice.
markdown-it is a JavaScript Markdown parser that you extend with plugins. A table of contents plugin turns a placeholder in your Markdown into a linked outline of the document's headings when the document is rendered. This review covers what that changes, where the result holds up, and when a manually written list of links is the safer choice. It draws on the plugins' documented behavior and general principles. No test results were supplied with this brief, so option names, defaults and output markup should be confirmed against the README of the version you install.
Is a markdown-it TOC plugin suitable for your use?
It is a good fit when you control the rendering pipeline. Examples include a static site generator, a documentation build or an application that calls markdown-it itself. The main benefit is that the outline is regenerated from the actual headings on every render, so it cannot drift out of date the way a hand-maintained list can.
The decisive tradeoff is that the table of contents exists only in the rendered HTML. The Markdown source keeps the placeholder, not the list. Any reader or platform that does not run your configured markdown-it instance sees the literal marker text and no outline. This includes a code host's file preview, a different Markdown renderer or someone reading the raw file.
Plain text is preferable in these cases:
- The file will mostly be read raw or on platforms with their own renderer.
- The document is short enough that an outline adds little.
- You need the table of contents to survive being pasted into another editor or tool.
- You cannot control or pin the plugin versions used to build the output.
- In these cases, either write a short list of links by hand or use a tool that inserts a static list into the Markdown file itself.
What the transformation actually changes
In this context, the word "generate" covers two different approaches.
markdown-it plugins work at render time. The source file is not edited. During parsing, the plugin finds the placeholder and replaces it in the output with a nested list of links to the document's headings.
Source-insertion tools work differently. They rewrite the Markdown file and write a real list into it, usually between marker comments. Their result is stored characters that any renderer can display.
Linking to headings requires that each heading has an id in the HTML. That is usually the job of a separate plugin, markdown-it-anchor. It adds id attributes to headings and can optionally add permalink symbols. TOC plugins such as markdown-it-table-of-contents and markdown-it-toc-done-right are commonly paired with it. The combined effect on output is two-fold:
- Heading elements gain id attributes derived from their text by a slug function.
- The placeholder is replaced by a list of in-page fragment links, usually inside a container element with a class you can style.
The heading text itself is not altered. The same applies to every character in your prose.
The supporting search terms cover packages with similar names, which have different placeholder syntax and options. The markdown-it-table-of-contents plugin documents a [[toc]] marker and an option for which heading levels to include. markdown-it-toc-done-right documents a ${toc} marker style and wraps its output in a nav element. An older package published simply as markdown-it-toc also exists. Check each package's maintenance status and its documented marker before relying on it, rather than assuming the names are interchangeable.
The following is an illustrative setup for markdown-it-table-of-contents. It has not been run for this article, and option names should be checked against your installed version:
import markdownIt from 'markdown-it';
import anchor from 'markdown-it-anchor';
import toc from 'markdown-it-table-of-contents';
const md = markdownIt()
.use(anchor)
.use(toc, { includeLevel: [2, 3] });
const html = md.render('[[toc]]\n\n## Install\n\n## Usage\n');
With this setup, the expected result is a list linking to the Install and Usage headings, with ids added to those headings. Render a sample and inspect the HTML to confirm the exact markup, class names and slugs your versions produce.
Slugs: where links most often go wrong
Every link in the table of contents depends on the slug function that turns heading text into an id. Several details are worth checking.
- Non-ASCII text and punctuation. Depending on the slug function, headings with accented letters, non-Latin scripts, emoji or symbols may produce percent-encoded ids or ids with characters stripped. Test headings representative of your content.
- Matching another platform. If links must match ids generated elsewhere, such as a code host's heading anchors or existing inbound links, supply a custom slug function that reproduces that scheme. Do not assume the defaults match.
- Duplicate headings. Two headings with the same text need distinct ids. Check how your anchor plugin disambiguates them, and note that the result depends on heading order.
- Edited headings. Because ids come from heading text, renaming a heading changes its id. That silently breaks any shared or bookmarked link to the old fragment, even though the generated table of contents itself stays correct.
Will the result stay readable and usable after copying or sharing?
| Situation | What to expect | Plain-text equivalent needed? |
| Viewing the rendered page | A linked, nested outline that updates with the headings | No |
| Reading the raw Markdown | Only the placeholder marker appears | Yes, if raw readers matter |
| Another Markdown renderer | Placeholder shown as literal text unless that renderer has a compatible plugin | Usually yes |
| Copying the rendered outline into another app | Varies by destination; link targets may be dropped or point to the original page | Test the specific destination |
| Sharing a deep link to a section | Works while the heading text is unchanged | Consider stable, explicit ids for important sections |
The table above describes expected behavior from how the plugins work, not tested results for any named application. Behavior when pasting into a specific editor, chat tool or content system should be checked in that destination before you depend on it.
Accessibility depends on the markup the plugin emits and on your heading structure. A nested list of links conveys hierarchy to screen readers. A nav element with an accessible label lets assistive technology users jump to the outline as a landmark. Some plugins emit nav by default and others use a generic container, so inspect your output and add a label if needed. Skipped heading levels, such as jumping from level 2 to level 4, can produce confusing nesting in the outline. Fixing the heading structure in the source is better than working around it in the plugin. No screen reader testing was supplied for this review, so verify with the assistive technology your audience uses.
For search and editing, the plugin leaves heading text and body content untouched, and it adds only in-page links. Authors continue editing ordinary Markdown. The one editing cost is that heading changes affect ids, as described above.
Bottom line
Use a markdown-it TOC plugin, paired with an anchor plugin, when your Markdown is always rendered through your own configured pipeline. Before publishing, check slugs against your real headings and inspect the emitted markup for accessibility. If the document is also read raw, on other platforms or after being copied elsewhere, keep a plain written list of links or use a tool that stores the list in the file, because a render-time outline does not travel with the source.