Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.


Build Coverage Downloads Size Sponsors Backers Chat

remark plugin to generate a table of contents.


This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.


npm install remark-toc


Say we have the following file,

# Alpha

## Table of Contents

## Bravo

### Charlie

## Delta

And our module, example.js, looks as follows (note: install all these dependencies!):

import {readSync} from 'to-vfile'
import {remark} from 'remark'
import remarkToc from 'remark-toc'

const file = readSync('')

  .then((file) => {

Now, running node example yields:

# Alpha

## Table of Contents

-   [Bravo](#bravo)

    -   [Charlie](#charlie)

-   [Delta](#delta)

## Bravo

### Charlie

## Delta


This package exports no identifiers. The default export is remarkToc.

unified().use(remarkToc[, options])

Generate a table of contents.

  • Looks for the first heading containing 'Table of Contents', 'toc', or 'table-of-contents' (case insensitive, supports alt/title attributes for links and images too)
  • Removes all following contents until an equal or higher heading is found
  • Inserts a list representation of the hierarchy of following headings
  • Links from the table of contents to following headings, using the same slugs as GitHub

Note: if you’re later compiling to HTML, you still need to add anchors to headings. Previously that was done by this plugin as well, but now you must .use(slug) to include remark-slug explicitly.


All options are passed to mdast-util-toc, with the exception that heading defaults to 'toc|table[ -]of[ -]contents?'.


Use of remark-toc involves user content and changes the tree, so it can open you up for a cross-site scripting (XSS) attack.

Existing nodes are copied into the table of contents. The following example shows how an existing script is copied into the table of contents.

The following Markdown:

# Table of Contents

## Bravo<script>alert(1)</script>

## Charlie


# Table of Contents

-   [Bravo<script>alert(1)</script>](#bravoscriptalert1script)
-   [Charlie](#charlie)

## Bravo<script>alert(1)</script>

## Charlie

This may become a problem if the Markdown is later transformed to rehype (hast) or opened in an unsafe Markdown viewer.



See in remarkjs/.github for ways to get started. See for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.


MIT © Titus Wormer