Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including remark-toc with all npm packages installed. Try it out:

var remarkToc = require("remark-toc")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

remark-toc v7.0.0

remark plugin to generate a Table of Contents (TOC)


Build Coverage Downloads Size Sponsors Backers Chat

remark plugin to generate a Table of Contents.



npm install remark-toc


Say we have the following file,

# Alpha

## Table of Contents

## Bravo

### Charlie

## Delta

And our script, example.js, looks as follows:

var vfile = require('to-vfile')
var remark = require('remark')
var toc = require('remark-toc')

  .process(vfile.readSync(''), function(err, file) {
    if (err) throw err

Now, running node example yields:

# Alpha

## Table of Contents

-   [Bravo](#bravo)

    -   [Charlie](#charlie)

-   [Delta](#delta)

## Bravo

### Charlie

## Delta


remark().use(toc[, 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.


string?, default: 'toc|table[ -]of[ -]contents?' — Heading to look for, wrapped in new RegExp('^(' + value + ')apos;, 'i').


number?, default: 6 — Maximum heading depth to include in the table of contents, This is inclusive, thus, when set to 3, level three headings, are included (those with three hashes, ###).


boolean?, default: false — Whether to compile list-items tightly.


string? — Headings to skip, wrapped in new RegExp('^(' + value + ')apos;, 'i'). Any heading matching this expression will not be present in the 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, organisation, or community you agree to abide by its terms.


MIT © Titus Wormer


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free