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
| Setting | Choices |
|---|---|
| Include headings down to | H3 (###) by default, or H2, H4 or All levels. Deeper headings are left out of the list. |
| Top-level # heading | Leave out by default, because a single # heading is usually the document title. Choose Include if your file uses several # headings as chapters. |
| List style | Bullets (-) or Numbered (1.). |
| Heading above the list | Adds "## 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?
How are Markdown heading anchors created?
What happens with two headings that have the same name?
Why is my # title not in the table of contents?
Does it pick up headings inside code blocks?
Can I make a numbered table of contents?
Related guides
- Free Developer Tools That Replace a Dozen Browser Tabs Free developer tools grouped by job: format, convert, encode, generate and test. What each one does, which run on the server, and the limits to know f...
- How to Beautify CSS, HTML and XML (Online, VS Code, Prettier) Beautifying adds line breaks and indentation so minified CSS, HTML or XML is readable again. How the free formatters work, their limits, and VS Code a...
- How to Format JSON: Pretty-Print, Validate and Read Errors Paste JSON into a formatter to indent it and catch errors. The exact syntax rules, how to read an error position, and how to do it in VS Code, Python...