What is a Markdown table of contents?
A table of contents (TOC) for a Markdown file is a list of links to its headings. In a long README, a specification or a wiki page it lets readers jump straight to the part they need. GitHub shows an outline of its own on the side of rendered files, but that outline is not part of the file: if you want a list inside the document, or on a site that does not draw one, you have to write it. Doing that by hand is easy to get wrong, because every link needs the exact anchor the renderer gives to the heading.
This tool reads your Markdown, finds the headings and writes the list for you. It understands the two heading styles of Markdown: ATX headings that start with #, and Setext headings, which are a line of text underlined with === or ---. It ignores anything that only looks like a heading, such as a comment line inside a fenced code block, text in an HTML comment, or the front matter block at the top of a file.
The anchors follow the algorithm GitHub uses (the one in the github-slugger library): the title is lower-cased, punctuation and emoji are dropped, letters with accents and non-Latin scripts are kept, spaces become hyphens, and a repeated title gets -1, -2 and so on. Everything runs in your browser and your document is never uploaded.
How to use it
- Open the page. A sample README is already loaded, with a pair of
<!-- toc -->and<!-- tocstop -->markers at the top. - Paste your own Markdown on the left, or open a
.mdfile. The result updates as you type. - Pick the range of levels. For a README, starting at H2 is common, because H1 is the page title.
- Choose bullets or a numbered list. For bullets you can set the marker (
-,*or+) and two or four spaces per level. A numbered list indents by the width of its number. - Choose what to copy: the whole document, with the list placed between the markers, or the list alone. If the document has no markers, the tool adds them at the top and says so under the panels.
- To keep some titles out, such as the heading of the TOC itself, type them in the "leave out" field, separated by commas.
- Copy the result or download it, and run the tool again whenever the headings change. The text between the markers is replaced each time, so the result does not pile up.
Examples
A simple README
Input:
# Acme CLI
## Installation
## Usage
### OptionsWith the range H1 to H6 the list is:
- [Acme CLI](#acme-cli)
- [Installation](#installation)
- [Usage](#usage)
- [Options](#options)Repeated titles and accents
Two sections called "Installation" give #installation and #installation-1. A title such as "Café & Crème" becomes #café--crème: the ampersand is removed, but both spaces around it remain, so there are two hyphens. This is how GitHub does it, and it is the most common reason hand-written TOCs break.
Setext headings and code blocks
A line followed by ----- is a level 2 heading and is listed. A line starting with # inside a fenced code block, such as a shell comment, is not a heading and is left out.
A numbered outline
Switch to numbered and the same document gives 1., then 1. nested under it for each child, restarting the count for every parent. The anchors do not change.
Common mistakes
- Expecting the same anchors on every site. The anchors match GitHub. GitLab, Docusaurus, MkDocs and other generators use slightly different rules, so check the result where you publish.
- Hiding levels and losing numbers. When you filter the levels, the anchors of repeated titles are still counted over all headings, as GitHub does. A second "Usage" at a hidden level still makes the next one
usage-1. - Headings without a space.
#Titleis not a heading in standard Markdown. It needs a space after the hashes. - Links inside titles. A link in a heading is flattened to its text in the list, since a link inside a link would not work. Emphasis and code in titles are kept.
- Markers in the wrong place. The closing marker may be
<!-- tocstop -->or<!-- /toc -->. If only the opening one exists, the tool inserts a closing one for you. - Titles that are only symbols. A heading made only of emoji produces an empty anchor, which is what GitHub does too.
Frequently asked questions
Does it change my headings?
No. It only produces the list, and in document mode it replaces what sits between the markers. Everything else is kept as it was.
Does it work with front matter?
Yes. A block between --- lines on the very first line is skipped, so keys and comments in it are never read as headings.
Which heading levels exist?
Levels 1 to 6, the same as Markdown. A line with seven hashes is plain text.
Can I turn the result into a page?
Use Markdown to HTML to check how it renders, and compare texts to see what changed after regenerating.
How do I build the Markdown in the first place?
Use Text to Markdown for plain text or Paste as Markdown for content copied from a page or a word processor.