GitHub Markdown Table of Contents Generation
How a table of contents works in GitHub Markdown, how heading anchors are formed, where generated links break, and when the built-in outline is enough.
Short answer: when a generated table of contents is worth it
For a README or documentation file long enough that readers scroll to find a section, a table of contents made of anchor links is worth adding. It lets readers jump straight to a section, and it shows the file's structure at the top of the raw file as well as the rendered page. There are two costs. The list must stay in sync with the headings, and its links only work while the anchors follow GitHub's rules. For short files, files with only a few headings, or content read mostly outside GitHub, plain headings and GitHub's built-in outline are usually enough.
What GitHub provides without any generator
When GitHub renders a Markdown file, it gives each heading an anchor. Hovering over a heading shows a link icon, and that link points directly to the section. The file view also has an outline menu built from the headings. Both features are presentation only. They are not stored in the file, so they do not appear in the raw text, in a local clone, or in another Markdown renderer. GitHub Flavored Markdown has no table-of-contents tag that expands into a list. A marker such as [TOC] or [[TOC]] from another platform shows up on GitHub as literal text.
What inserting a table of contents changes
A generator reads the file's headings, works out the anchor for each one, and writes a nested Markdown list of links into the file. It does not change any existing characters. The headings, their wording and the document's meaning stay the same, and new text is added. Many generators wrap their output in HTML comment markers so they can find and replace the list later. GitHub does not display these comments, but they stay in the raw file.
<!-- toc -->
- [Getting Started](#getting-started)
- [Installation](#installation)
- [Usage](#usage)
- [FAQ & Troubleshooting](#faq--troubleshooting)
- [Usage](#usage-1)
<!-- tocstop -->
Each anchor in this example follows from the heading text. "Getting Started" becomes getting-started. In "FAQ & Troubleshooting" the ampersand is removed and both spaces around it become hyphens, which leaves a double hyphen. The second heading named "Usage" gets a numeric suffix so it stays distinct from the first. Indentation in the list mirrors heading levels, so a nested item belongs under the heading above it.
How GitHub turns headings into anchors
- Letters are converted to lowercase.
- Spaces are replaced with hyphens.
- Most punctuation is removed. Hyphens and underscores are kept.
- Emoji and other symbols are dropped, but letters from non-Latin scripts are kept.
- If a heading's anchor duplicates an earlier one, it gets -1, -2 and so on, counted in document order.
These are GitHub's documented conventions, not a universal Markdown standard, and other platforms form anchors differently. The most reliable check is to copy the link from the rendered heading's link icon and compare it with what the generator wrote.
Where generated tables of contents break
| Situation | What happens | What to do |
| A heading is renamed after the list is generated | The link points to an anchor that no longer exists, and GitHub gives no warning | Regenerate the list, or check it automatically before merging |
| Duplicate headings | Suffix numbers depend on order, so moving sections changes them | Give sections unique headings where possible |
| Headings with emoji, punctuation, inline code or links | The generator's slug may not match GitHub's | Compare with the link from the rendered heading |
| Headings in non-Latin scripts | A generator that strips non-ASCII letters produces wrong anchors | Test a sample heading before relying on the tool |
| The same file is published elsewhere, such as a documentation site | Different anchor rules may break some or all links | Use that platform's own contents feature or test the links there |
Readability, copying and accessibility
In the raw file, a table of contents stays readable. It is an ordinary list in which each entry repeats a heading inside link syntax. When the rendered list is copied from a browser, the result depends on where it is pasted. Rich-text editors may keep the links, which point back to the GitHub page. Plain-text fields keep only the words. This review did not test specific destinations, so check the target before relying on either behavior.
A table of contents built as a standard nested list of links gives screen reader users a list structure and descriptive link text, provided the link text matches the headings. Avoid decorative choices that read badly aloud or break anchors, such as emoji-only entries or headings set in Unicode "font" characters. Those styled letters are separate code points, not formatting. Assistive technology may announce them oddly or skip them, search may not match them, and they can produce unexpected anchors. Keep heading levels in order as well. Skipped levels create confusing nesting in the list and in the document outline. These points come from general principles, not from tests with particular screen readers.
Some destinations do not render Markdown, such as plain-text email, terminal output or a plain-text field. In those places the brackets and parentheses are noise, so a plain list of section names works better.
Choosing an approach
- Built-in outline only: nothing to maintain and always current, but available only on GitHub's rendered view.
- Hand-written list: no tooling needed, but it drifts out of date as headings change.
- Editor-based generator: updates when the author runs it, so accuracy depends on remembering to run it.
- Generator run in a pre-commit hook or automated check: keeps the list in sync with less effort, at the cost of setup and one more dependency to maintain.
What this review could not verify
No tool-specific evidence was supplied, so this article does not name, rank or measure individual generators and makes no claims about their features, privacy or data handling. The anchor rules above describe GitHub's documented behavior, which can change. Statements about other renderers, copy-and-paste destinations and assistive technology are general and should be checked in the environment where the file will actually be read.