← Back to blog/Guides

Markdown Table of Contents: Heading Links That Keep Working

Build a useful Markdown table of contents, verify heading IDs in the destination, and avoid fragile links when headings change.

F
Faizan Khan
• 5 min read

A manual Markdown table of contents is a list of links to the document's headings, such as [Recovery](#recovery). It works only when the renderer gives the destination heading a matching ID. CommonMark defines headings and links but does not specify automatic heading IDs or a universal [TOC] command.

Markdown
1## Contents
2
3- [Before you begin](#before-you-begin)
4- [Export the files](#export-the-files)
5- [Check the result](#check-the-result)

Start with a small outline, then verify the actual heading permalinks in the published destination. Do not assume that every Markdown engine derives fragments the same way.

A complete worked document

This example uses plain headings and links with simple names:

Markdown
1# Export review guide
2
3Use this procedure to prepare and review an export.
4
5## Contents
6
7- [Before you begin](#before-you-begin)
8- [Export the files](#export-the-files)
9 - [Choose a destination](#choose-a-destination)
10- [Check the result](#check-the-result)
11
12## Before you begin
13
14Confirm that the destination folder is available.
15
16## Export the files
17
18Create the export from the approved source.
19
20### Choose a destination
21
22Use a folder that the reviewer can access.
23
24## Check the result
25
26Open the exported file and confirm the expected content.

The nested TOC item reflects a level-three heading within "Export the files." It is not nested merely to make the list look decorative. The outline and the document hierarchy agree.

On a renderer using the expected IDs, #choose-a-destination navigates to the corresponding subsection. A renderer that emits bare h3 elements without IDs will display the heading but cannot satisfy that link automatically.

For list indentation, use the Markdown lists guide. For the general difference between path and fragment destinations, see Markdown links.

How GitHub section links differ from a universal rule

GitHub creates section anchors from heading text. Its section-link documentation describes lowercasing, space replacement, punctuation handling, and duplicate-name suffixes.

Those rules are useful when the document is meant for GitHub. They are not a CommonMark guarantee. A documentation framework might use another slugging library, preserve different characters, add prefixes, or support explicit IDs through a separate syntax.

The safest workflow is to render the destination, copy the heading's permalink, and use its fragment. For headings with punctuation, code spans, or non-Latin characters, this avoids guessing which characters survive.

Do not replace readable headings with awkward keyword strings just to simplify an anchor. Prefer meaningful headings and, where supported, a stable explicit ID.

Avoid ambiguous repeated headings

This outline creates a maintenance problem:

Markdown
1## Windows
2### Troubleshooting
3
4## Linux
5### Troubleshooting

The headings may receive IDs such as troubleshooting and troubleshooting-1 on GitHub. Insert another matching heading earlier, and a later suffix can change.

Prefer distinguishing the headings:

Markdown
1## Windows
2### Windows troubleshooting
3
4## Linux
5### Linux troubleshooting

The wording is clearer in a TOC and less sensitive to insertion order. If the destination supports custom anchors, you can use them deliberately, but spelling such as {#custom-id} is itself an extension. Check support rather than adding it to every Markdown file.

Manual TOC versus generated navigation

ApproachAdvantageReview obligation
Manual list of heading linksWorks with ordinary link syntaxUpdate labels and fragments after heading changes
Build-time generated TOCCan follow the parsed outline automaticallyVerify heading depth, exclusions, and IDs
Platform document outlineAvoids maintaining a second inline listConfirm readers can discover and use it
Renderer-specific TOC tokenConvenient on that platformConfirm support before moving the source elsewhere

Tokens such as [TOC] and [[toc]] are not interchangeable standards. If the token appears as literal text, the renderer probably lacks the corresponding feature.

Avoid placing both a long inline TOC and a large automatic outline on a short article. Navigation should reduce work, not create another screen of repeated headings.

Review headings as navigation labels

"Overview," "Details," and "More" are vague when repeated. "Choose an export destination" and "Recover a failed export" tell the reader what they can accomplish.

Keep levels hierarchical. A level-two section can contain level-three subsections; do not choose level five solely because its font looks smaller. This supports a coherent reading order as well as a useful outline.

For a long reference, include major tasks rather than every tiny heading. A TOC should be a map, not a second full copy of the article. The Markdown cheat sheet shows how document structure fits alongside other syntax.

A heading-change acceptance check

After renaming or moving a heading, render the final page and open every TOC link. Confirm that each lands on the intended section, including repeated names. Test a direct URL containing the fragment in a fresh load, not only clicking within an already open page.

Also check incoming links from other articles when a public heading changes. Keeping the page URL unchanged does not preserve every fragment URL. Where the platform supports stable IDs or compatibility anchors, retain old published targets deliberately.

FAQ

Does Markdown automatically create a table of contents?

Core CommonMark does not. Platforms can provide generated outlines or plugins, while a manual TOC uses ordinary links to heading IDs.

Why does my table-of-contents link do nothing?

The fragment may not match an actual heading ID, or the renderer may not create heading IDs. Inspect the rendered permalink and test that exact target.

Is the TOC syntax the same on GitHub and every docs site?

No. Basic link syntax is shared, but heading-ID generation, custom anchor syntax, and automatic TOC tokens vary by renderer.

Continue This Guide