The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Auto-update table of contents in Markdown

To automatically update a table of contents in Markdown, you generally rely on specific tools or text editors that support this feature, such as Visual Studio Code with a Markdown extension, or by using GitHub‘s built-in support for auto-generating a TOC. Below are a few methods to achieve this:

1. Using Visual Studio Code with Markdown Extensions

  • Install the Markdown All in One extension in Visual Studio Code.

  • After the extension is installed, you can use a command to generate or update the table of contents.

  • The table of contents will be auto-updated every time you modify the headings in your Markdown file.

  • You can trigger the TOC update using the command palette (Ctrl+Shift+P) and selecting Markdown All in One: Create Table of Contents.

2. Using GitHub (with Automatic TOC)

GitHub automatically updates a table of contents if you use the following syntax in a Markdown file:

markdown
**Table of Contents** - [Introduction](#introduction) - [Main Topic](#main-topic) - [Conclusion](#conclusion)

However, GitHub won’t auto-generate or update it for you. You need to manually keep the TOC in sync with the headings, or use a script/extension to help with this.

3. Using Markdown TOC Generator (Tool)

You can use a Markdown TOC generator like markdown-toc to auto-generate and update the TOC. Here’s how:

  • Install markdown-toc using npm:

    bash
    npm install -g markdown-toc
  • Run the following command to generate and update the TOC in your Markdown file:

    bash
    markdown-toc -i README.md
  • This will auto-update the table of contents based on the headers in your Markdown file.

4. Manual TOC Generation (Markdown Syntax)

Although this isn’t “automatic”, here’s the basic syntax to create a table of contents manually in Markdown:

markdown
**Table of Contents** - [Introduction](#introduction) - [Getting Started](#getting-started) - [Features](#features) - [Conclusion](#conclusion)

Each link in the table of contents corresponds to a section header, and the anchor is the section title in lowercase, with spaces replaced by hyphens.

Would you prefer using an automated tool, or do you need guidance on something else?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About