Generate a Table of Contents in Markdown
How Markdown table of contents generation works, what it changes in your file, and when anchor links break after copying or sharing.
A table of contents in a Markdown document is usually a list of links that point to the document's own headings. You can type one by hand, insert one with a generator tool, or let the rendering platform build one when the page is displayed. Each approach has different effects on your source file and on how well the result survives being copied, converted or shared. This review explains those effects so you can choose an approach that fits your use.
Is a generated table of contents right for your document?
A generated table of contents is a good fit for long Markdown documents that are read in a single known renderer. Examples include a README on one code-hosting platform, a documentation site built with one static site generator, or a Pandoc export. The main benefit is navigation: readers can see the structure and jump to a section without scrolling. Generating the list also avoids hand-typing anchor links, which is where most errors occur.
The decisive tradeoff is portability. Anchor links depend on how a specific renderer turns heading text into identifiers. Placeholder markers that expand into a table of contents work only where they are supported. A table of contents that works on one platform can be partly or completely broken on another.
A plain-text outline, or no table of contents at all, is often the better choice in these cases: the document is short; it will be pasted into chat, email or a word processor; it moves between renderers with different heading rules; or the platform already shows its own outline of the headings. In those cases, a static list of section names without links avoids broken links and still shows the reader the structure.
What generating a table of contents actually changes
A generator reads the document's headings, usually lines that start with one or more # characters. It records each heading's level and text and builds a nested list. Each list entry links to a fragment identifier, written after a # in the link target, which the renderer is expected to attach to the matching heading. Heading levels become nesting levels, and many tools let you limit which levels are included.
The important distinction is whether the table of contents is stored in your file or produced only when the document is displayed:
- Stored: a command-line tool, editor extension or script writes the list of links directly into the Markdown source. The file's characters change, the list appears as ordinary Markdown in any editor, and it must be regenerated when headings change. Many such tools put comment markers around the list so they can find and replace it later.
- Rendered: you write a placeholder marker, or nothing at all, and the renderer builds the list at display time. Some Markdown processors and hosting platforms support a marker of this kind, and some converters, such as Pandoc with its table-of-contents option, generate the list during conversion. The source file does not contain the list, so other tools will not show it.
- Platform outline: some hosting interfaces show a heading outline in their own interface, separate from the document. The file is unchanged, and the outline exists only on that platform.
Below is an illustrative stored table of contents for a document with headings named Installation, Install & Setup, Usage and FAQ. The fragment identifiers follow GitHub-style rules: lowercase the text, remove most punctuation, and replace spaces with hyphens. Under those rules, removing the ampersand from "Install & Setup" leaves two adjacent spaces, which become a double hyphen. Other renderers may produce different identifiers from the same heading.
## Contents
- [Installation](#installation)
- [Install & Setup](#install--setup)
- [Usage](#usage)
- [FAQ](#faq)
Identifier rules are where renderers differ most. They vary in how they handle punctuation, accented and non-Latin letters, emoji, leading numbers and repeated headings. Some renderers add a numeric suffix to the second and later occurrences of a duplicate heading, so two sections named "Examples" get different identifiers. Generators usually try to copy the rules of one target platform. Check which platform a tool targets before you rely on its links.
Words like "reverse", "mirror" and "font" have no special meaning in table of contents generation. Generators do not reverse or restyle text, and "Markdown TOC font" settings usually refer to renderer or theme CSS, not to the Markdown file. One related caution does apply. If a heading uses Unicode look-alike "font" characters, such as mathematical bold or italic letters in place of ordinary letters, the stored characters are different code points. Slug rules may then remove or keep those characters in ways you cannot predict, which can produce empty or unreadable identifiers. Use ordinary text in headings and apply styling with Markdown emphasis or CSS instead.
Will it stay usable after copying or sharing?
Copying the rendered page into another application usually keeps the visible list text, but the links may not survive. If they do survive, they may point to fragments that do not exist in the new location. Copying the Markdown source into a different renderer keeps the links, but they only work if the new renderer creates the same heading identifiers. A placeholder marker copied to a platform that does not support it will usually appear as literal text, or as nothing. No cross-platform compatibility testing was supplied for this review, so test the specific destination before you share the document widely.
Stored tables of contents can go out of date. When a heading is renamed, its identifier usually changes, but the stored link does not, unless you regenerate the list or a hook or build step does it for you. Rendered tables of contents avoid this problem, but they only exist where the renderer runs.
For accessibility, a table of contents written as a nested list of descriptive links is generally a reasonable pattern. Assistive technology can announce it as a list and move through its links. It is an addition to well-structured headings, not a substitute for them, because many screen reader users navigate by heading directly. Keep heading levels in logical order, and avoid look-alike Unicode characters in headings because assistive technology may announce them oddly or skip them. No screen reader testing of specific generators or platforms was supplied, so these points describe general principles, not verified behavior for any particular tool.
Search and editing are mostly unaffected. The headings remain ordinary text, and a stored list is ordinary Markdown that any editor can change. The main editing cost is avoiding conflicts with a generator: if a tool manages the area between its markers, manual edits there may be overwritten the next time the tool runs.
You need a plain-text equivalent when the destination does not render Markdown links, when the identifier rules are unknown, or when readers will see the content outside its original platform. A simple list of section titles without link targets keeps the document structure readable everywhere and cannot point to the wrong section.
Checks before relying on a generated table of contents
- Identify the renderer where readers will actually see the document, and choose a generator that targets its heading identifier rules.
- Decide whether you want the list stored in the file or produced at render time. Prefer a stored list if the document will be read in more than one place.
- Preview the rendered result and click every link, especially for headings with punctuation, non-Latin letters, emoji or duplicate names.
- Use ordinary characters in headings, not Unicode look-alike styled letters.
- Set a way to keep stored lists current, such as regenerating before each commit or as part of a build step.
- For content that will be pasted elsewhere, provide a plain list of section names instead of links.
What this review could not verify
No tool-specific documentation, version details, test results or measurements were supplied. This review therefore does not rank generators or confirm how any named platform currently handles identifiers, markers, emoji or assistive technology. The slug example above illustrates GitHub-style rules and is not a guarantee for any renderer. Privacy and data handling of online generators were also not assessed. Check the documentation of the tool and platform you plan to use, and test in your actual destination.