Make a table of contents from Markdown headings

Build a linked table of contents from a Markdown file's headings, with anchors made the way GitHub makes them, nested by level and skipping code blocks.

How to make a Markdown table of contents

  1. 1

    Paste the Markdown or open the file

  2. 2

    Choose the deepest heading level to list

  3. 3

    Copy the list into your document

A contents list whose links work on GitHub

A long README or set of notes is easier to use with a list of its sections at the top. This reads the headings in your Markdown and writes that list, each entry a link to its heading, indented by level.

Which lines are headings

Headings are found the way the CommonMark specification defines them. A line starting with one to six # characters and a space is a heading of that level, and a closing run of # is dropped. A line of text underlined with equals signs is a level 1 heading, and one underlined with hyphens a level 2 heading. Lines inside fenced code blocks, between ``` or ~~~ fences, and lines indented four spaces are code and are skipped. A # with no space after it, as in #hashtag, is text.

The text shown in the list is the heading as it reads once rendered: links keep their words, code keeps its contents, and emphasis markers are removed.

The anchors

When GitHub renders Markdown, each heading gets an id the link can point to. The rule comes from the github-slugger package: lower-case the text, delete punctuation and symbols (keeping letters in any script, figures, hyphens, underscores and spaces), and replace each space with a hyphen. Headings that come out the same get -1, -2 and so on, in order. Other sites that render Markdown may make anchors differently, so check a link or two there.

Shaping the list

Choose the deepest level to include; level 3 is a common choice for a README. Entries nest two spaces per level, starting from the shallowest heading kept, and a skipped level nests one step rather than two, so the list stays valid Markdown. Tick “Number the entries” for an ordered list.

To turn the document into a web page, use Markdown to HTML.

Nothing is uploaded. The document is read in this tab.

Everything you need

GitHub's anchors

Lower case, punctuation removed, spaces to hyphens, and repeats numbered -1, -2, as github-slugger does.

Real headings only

ATX and underlined headings as CommonMark defines them, skipping code blocks and indented code.

Private by design

The document is read in your browser and never sent anywhere.

Nothing uploaded: open the Network panel while it runs and check.

No daily limits or watermarks: free batches take up to 20 images.

Works offline: once the page has loaded, on any current browser.

Frequently asked questions

How does GitHub make a heading's anchor?

It lower-cases the heading's text, removes punctuation and symbols except hyphens, and turns each space into a hyphen. "What's new in v2.0?" becomes whats-new-in-v20. A second heading with the same text gets -1 on the end, a third -2.

Are headings inside code blocks listed?

No. A line starting with

Why leave out the title?

A README usually opens with one

Does it change my document?

No. It writes the list only, for you to paste where you want it, usually under the title. Run it again after editing headings, since the anchors follow the heading text.

Sources

Related guides

Work with files privately and confidentlyPractical, sourced explanations for safer file conversion, sharing, and cleanup.