Skip to main content
HomeBlogMarkdown Table of Contents: Build Links That Match Your Headings

Markdown Table of Contents: Build Links That Match Your Headings

Create a Markdown contents list, verify heading anchors at the publishing destination, and avoid assuming that every renderer creates IDs or expands TOC markers.

Published:

Updated:

A Markdown table of contents is usually a list of links to sections in the same document. The link syntax is portable; the generation of heading IDs is an application feature. This distinction explains why a contents list may work on GitHub and fail in another viewer.

Start with heading structure

Use headings to describe sections before writing a contents list:

# Import handbook

## Requirements

Describe the input files.

## Import steps

Explain the workflow.

### Review the result

Describe the checks.

Keep the levels logical. A contents list can summarize that structure, but it cannot fix unclear or incorrectly nested sections. See the heading guide for the syntax.

Find the actual anchor

A fragment link such as [Requirements](#requirements) points to an HTML element whose id is requirements. Core Markdown does not require a heading to have an ID. A renderer can create an <h2> without an anchor.

GitHub documents its section-link rules, including how repeated headings receive different anchors. Other applications can handle punctuation, non-Latin characters, spaces, and duplicate titles differently.

The reliable procedure is to render the document at the destination, copy the section link supplied there, and test it. If no section-link control is available, inspect the heading's id in browser developer tools. Do not assume a generated slug from another tool will match.

The current MD File Viewer tool preview does not add heading IDs. It can help check the headings and list structure, but in-page contents links need to be tested on a destination that supplies the matching IDs. Articles on this website use a separate publishing pipeline that does add section IDs.

Write a manual contents list

Once the destination supplies these anchors, a list can look like this:

## Contents

- [Requirements](#requirements)
- [Import steps](#import-steps)
  - [Review the result](#review-the-result)

The list items are ordinary Markdown links. Indentation creates the nested item. Keep each link label meaningful and make sure every destination exists in the same document. A renamed heading can break a previously correct link even if the list still looks right.

Do not copy a long example whose linked sections are missing: a useful contents list should describe the actual document, not a planned structure.

Use automatic generation when it fits the project

Some editors and documentation frameworks generate a contents list from the headings. Their configuration, placeholder markers, and slug rules differ. Check the documentation for the exact tool and version you use.

Markers such as [TOC], _TOC_, and {#custom-id} are not universal Markdown features. In an unsupported renderer, they can remain visible text. GitHub provides its own outline interface for Markdown files; that does not mean it expands an arbitrary TOC marker inside the file.

If you use a generator that writes links back into the source, review its output and keep its anchor rules aligned with the destination. Regenerating the list is useful after adding, moving, or renaming sections. It is not a substitute for clicking the links.

Check the published document

  1. Open every top-level contents link.
  2. Test at least one nested item and any repeated heading names.
  3. Confirm that non-Latin titles and punctuation resolve correctly.
  4. Try the links after publishing at the final URL, rather than only from a local draft.
  5. Recheck after an export or a change of publishing platform.

A web page's contents links are not guaranteed to become PDF bookmarks or clickable links in another file format. The current direct PDF download is image based, so its visible contents list is not interactive. Review the PDF guide when you need navigable output.

For a short note, clear headings may be sufficient. Add a contents list when it helps readers jump between sections, and keep it short enough to scan.

Try it in your browser

Open the homepage editor to view, edit, and download Markdown without installing an application.

More articles

Markdown Table of Contents: Build Links That Match Your Headings | MD File Viewer