Resell services with the EthicalSMM API

Markdown TOC Generator

Generate a linked table of contents from the headings in a Markdown file or README, with GitHub-style anchors and nested lists.

Was this tool helpful?

Thanks for the feedback!

This generator reads the headings in a Markdown file and builds a linked table of contents, with GitHub-style anchors and nested lists. Paste a README, a docs page or a long note, choose how deep to go, and copy the list back into the top of the file. It is for developers maintaining READMEs and anyone writing long Markdown documents.

The settings

SettingChoices
Include headings down toH3 (###) by default, or H2, H4 or All levels. Deeper headings are left out of the list.
Top-level # headingLeave out by default, because a single # heading is usually the document title. Choose Include if your file uses several # headings as chapters.
List styleBullets (-) or Numbered (1.).
Heading above the listAdds "## Table of contents" above the list, or no heading.

Press Generate TOC. The card under the result shows how many headings were included.

How headings and anchors are worked out

The tool recognises two kinds of heading. The common kind starts with one to six # characters followed by a space, with at most three spaces of indent; closing # characters are dropped. It also recognises the older underline style, where a line of = marks the line above as level 1 and a line of - marks it as level 2. Lines inside fenced code blocks (``` or ~~~) are skipped, so a shell comment like ## install deps in a code sample does not end up in your table. #Heading with no space is not a heading.

The link text is the heading with formatting removed: links and images keep only their text, and the characters * _ ~ and backticks are stripped, as are HTML tags. Square brackets in the text are escaped so the link still works.

The anchor is made like this: lower-case the text, delete everything that is not a letter, digit, space, underscore or hyphen, then turn each space into a hyphen. If two headings give the same anchor, the second gets -1, the third -2, and so on, which is what GitHub does. Headings outside your chosen depth still count for this numbering, so the anchors match the real page.

The shallowest included level sits at the left. Each level below is indented by two spaces for bullets, or three spaces for numbered lists.

Worked example

Here is part of a README:

# Rail Pass App
## Getting started
### Install on Android
### Install on iOS
## Using the app
```bash
## this is a comment, not a heading
```
### Booking a ticket (Tatkal)
#### Payment via UPI
## FAQ
## FAQ
## What’s new in v2.0?
## `config.json` options

With the default settings the result is:

## Table of contents

- [Getting started](#getting-started)
  - [Install on Android](#install-on-android)
  - [Install on iOS](#install-on-ios)
- [Using the app](#using-the-app)
  - [Booking a ticket (Tatkal)](#booking-a-ticket-tatkal)
- [FAQ](#faq)
- [FAQ](#faq-1)
- [What’s new in v2.0?](#whats-new-in-v20)
- [config.json options](#configjson-options)

The # title is left out, the comment inside the code block is ignored, and "Payment via UPI" is skipped because it is an H4. The second FAQ gets the anchor #faq-1. Brackets, the apostrophe, the question mark and the dot are dropped from the anchors. Change the depth to H4 and the UPI heading appears, nested under the Tatkal one.

Things to check

If the tool says "No headings found in the range you chose. Markdown headings start with # at the beginning of a line.", the file may only have a # title while Top-level # heading is set to leave it out, or the headings are deeper than your chosen level.

Underscores used for emphasis, as in _new_, are dropped with the formatting, but underscores inside words stay, so a heading like "snake_case names" gets the anchor #snake_case-names, the same as on GitHub. To preview the finished file, paste it into Markdown to HTML; to add a data table to the same README, the CSV to Markdown Table converter helps.

Frequently asked questions

How do I add a table of contents to a GitHub README?
Paste the README here, press Generate TOC and copy the list into the file below the title. GitHub turns each heading into an anchor, so the links jump to the right section.
How are Markdown heading anchors created?
The heading text is lower-cased, punctuation other than hyphens and underscores is removed, and spaces become hyphens. "Getting started" becomes #getting-started.
What happens with two headings that have the same name?
The first keeps the plain anchor and later ones get a number, so two FAQ headings link to #faq and #faq-1.
Why is my # title not in the table of contents?
Top-level # heading is set to Leave out by default, because the single # heading is usually the page title. Switch it to Include to list it.
Does it pick up headings inside code blocks?
No. Lines between ``` or ~~~ fences are skipped, so comments in shell or Python samples are not mistaken for headings.
Can I make a numbered table of contents?
Yes. Set List style to Numbered (1.) and each level is numbered from 1, with sub-levels indented by three spaces.

Related guides

Related tools

People also used

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.