How to create a table of contents in Markdown

Build a Markdown table of contents by hand, with a generator or with a renderer marker, and check that every link reaches the right heading.

Core Markdown has no table-of-contents syntax. Neither the original Markdown description nor the CommonMark specification includes one. A Markdown table of contents is an ordinary list of links, and each link points to an anchor created from a heading. You can write that list yourself, let a tool insert it into the file, or use a marker that a specific renderer expands when it displays the page. The right choice depends mostly on where the file will be read.

What a Markdown table of contents does

The input is the document's headings. These can be ATX headings, which start with one to six # characters, or Setext headings, which are underlined with === or ---. The output is a nested list in which each entry links to a fragment such as #installation. A fragment is the part of a link after the # sign, and it jumps to an element on the same page that has a matching id. Markdown itself does not create those ids. The renderer does, for example a code-hosting site, a static site generator or a converter. A table of contents only works where the renderer adds heading ids, and the ids must match the slugs, which are the anchor names, used in your links.

Approach | What changes in your file | Good fit
Manual list | You type and maintain every link | Short documents that rarely change
Generator that edits the file (editor command or command-line tool) | A block of list text is inserted and later replaced between markers | Files read on platforms that do not build a table of contents automatically
Renderer marker (such as [TOC] or [[TOC]]) or a converter option | Only a marker or a build flag; the list is created at render time | Sites or builds where you control the renderer

Evidence gap: no tool documentation or tested output was supplied for this guide. Tool names, markers and commands below describe commonly documented behavior, and the examples are illustrations, not verified output. Check the documentation for the version you have before you rely on a specific option.

Before you start

  • Find out where the file will be displayed, because slug rules and marker support differ between renderers.
  • Finish or stabilize your headings first. Renaming a heading changes its anchor and breaks any link that points to it.
  • Decide which heading levels to include. Many tables of contents skip the single top-level title and list levels 2 and 3.
  • Commit the file to version control or make a copy before running any tool that edits it in place.

Option 1: Write the table of contents by hand

Steps: list the headings in document order, convert each heading to its anchor slug, write a link for each one, and indent subheadings beneath their parent heading. Here is an example document before a table of contents is added:

# Project Notes

## Installation

### Install & Configure

## Usage

## FAQ

Example after adding a manual table of contents below the title. The slugs follow GitHub-style rules and are not tested output:

# Project Notes

## Contents

- [Installation](#installation)
  - [Install & Configure](#install--configure)
- [Usage](#usage)
- [FAQ](#faq)

## Installation

### Install & Configure

## Usage

## FAQ

In GitHub-style slugs, the heading text is lowercased, each space becomes a hyphen, and most punctuation is removed. In "Install & Configure" the ampersand is dropped, but the spaces on either side of it each become a hyphen, which produces the double hyphen in #install--configure. When two headings have the same text, the second usually gets a numeric suffix such as -1. Other renderers may handle punctuation, case or duplicates differently, so check one generated id by inspecting the rendered page if you are unsure.

Nesting depends on indentation. In CommonMark, a sublist must be indented to the content column of its parent item. That means two spaces under an item that starts with "- " and three spaces under one that starts with "1. ". If you indent too little, the entry may appear as a sibling instead of a child.

Option 2: Generate it with a tool that writes into the file

Generators read your headings, compute slugs and write the list into the Markdown source. The resulting file works on any renderer whose slug rules match the generator's. In Visual Studio Code, extensions such as Markdown All in One provide a "Create Table of Contents" command that inserts the list at the cursor and can update it later. On the command line, the Node.js tools markdown-toc and doctoc are widely used.

markdown-toc fills in the area between a pair of HTML comment markers when you run it with the in-place flag. Example source before the run:

# Project Notes

<!-- toc -->

<!-- tocstop -->

## Installation
npx markdown-toc -i README.md

doctoc works in a similar way, but it adds its own start and end comment markers the first time it runs and updates the content between them on later runs:

npx doctoc README.md

Preservation: both tools rewrite the file in place, and anything between their markers is replaced on every run. Do not put hand-written notes inside the markers, and do not delete or edit the marker comments, or the next run may insert a second list instead of updating the first. If a run produces something unexpected, review the change and restore the previous version:

git diff README.md
git restore README.md

Option 3: Let the renderer build it

Some processors expand a placeholder into a table of contents at display time, so the source file only contains the marker. Python-Markdown's toc extension replaces [TOC]. GitLab documents [[TOC]] and [TOC] for its rendered Markdown, and Azure DevOps wikis use [[TOC]]. On any renderer that does not support the marker, it appears as literal text. Do not use this approach for a file that will be read in several places. GitHub's rendered file view does not expand these markers and provides its own outline control instead.

Pandoc builds a table of contents during conversion when you pass --toc together with -s, which produces a standalone document. --toc-depth limits which heading levels are included. Example:

pandoc notes.md -s --toc --toc-depth=2 -o notes.html

Text details that change the result

  • Lines starting with # inside fenced code blocks are not headings. A proper Markdown parser ignores them, but a quick regex or grep script will list them by mistake.
  • Setext headings (text underlined with === or ---) are real headings. Scripts that only look for a leading # will miss them.
  • Inline formatting in headings, such as code, emphasis or links, is usually stripped from the slug. Symbols may be removed entirely.
  • Non-ASCII letters such as é or ü are often kept in GitHub-style slugs, while emoji and most symbols are dropped. Renderers vary, so test a sample heading.
  • Optional closing # characters in ATX headings (## Usage ##) are not part of the heading text.
  • Save files as UTF-8. Line-ending conversion between CRLF and LF does not change the anchors, but it can make a regenerated table of contents appear as a whole-file change in a diff.

Check the result

  • Open the rendered page, not just the source, and click every entry to confirm it jumps to the matching heading.
  • Confirm the nesting matches the heading levels and that no heading is missing or duplicated.
  • Test headings that contain punctuation, repeated text or non-ASCII characters, because these are where slugs most often fail.
  • Review a diff of the file to confirm that only the table of contents block changed.
  • After renaming, adding or removing headings, regenerate or edit the list again, since anchors do not update on their own.