npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cocoa/eleventy-plugin-toc

v2.1.3

Published

Eleventy plugin for generating a table of contents from headings in your content, with many options for customisation.

Readme

@cocoa/eleventy-plugin-toc

[!NOTE] This repository is an updated fork of uncenter/eleventy-plugin-toc switching from a filter to a paired shortcode, and using @sindredorhus/slugify to guess at heading links if no IDs are present, for easier integration with Eleventy Id Attribute plugin. NB: This is a very brittle way to make a Table of Contents, and will break when you have repeated (sub)headings and if your slugify functions don't match perfectly.

[!IMPORTANT] This plugin is now ESM-only. It can be used in CJS though (i.e. imported via require) starting with Node.js version 20 and newer.

Easily generate a table of contents (TOC) for your Eleventy site, with easy configuration and customization.

HTML:

<h1>Hello, World</h1>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.

<h2 id="greetings-from-mars">Greetings from Mars</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.

<h3>The red planet</h3>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.

<h2 id="greetings-from-pluto">Greetings from Pluto</h2>

Generated TOC:

<nav class="toc">
  <ol>
    <li><a href="#greetings-from-mars">Greetings from Mars</a></li>
    <ol>
      <li><a href="#the-red-planet">The red planet</a></li>
    </ol>
    <li><a href="#greetings-from-pluto">Greetings from Pluto</a></li>
  </ol>
</nav>

Install

npm i @cocoa/eleventy-plugin-toc
pnpm add @cocoa/eleventy-plugin-toc
yarn add @cocoa/eleventy-plugin-toc
bun add @cocoa/eleventy-plugin-toc

Usage

Your heading elements will need to have id attributes on them, so that the table of contents can provide anchor links to them. Eleventy does not do this for you automatically, but you can use the provided Eleventy Id Attribute Plugin to add id attributes to the headings automatically.

[!IMPORTANT] Make sure not to duplicate the export default function (eleventyConfig) { line in your config file for any of the examples below! If you already have a export default function (eleventyConfig) { line, just add the lines above and below it to your config file.

In your Eleventy config file (.eleventy.js, eleventy.config.js, or eleventy.config.cjs), add your heading plugin of choice. This example uses the aforementioned IdAttributePlugin plugin:

import IdAttributePlugin from '@11ty/eleventy';
import slugify from '@sindresorhus/slugify';

export default function (eleventyConfig) {
  eleventyConfig.addPlugin(IdAttributePlugin, { slugify });
}

Then add the table of contents plugin:

import IdAttributePlugin from '@11ty/eleventy';
import pluginTOC from '@cocoa/eleventy-plugin-toc';
import slugify from '@sindresorhus/slugify';

export default function (eleventyConfig) {
  eleventyConfig.addPlugin(IdAttributePlugin, { slugify });
  eleventyConfig.addPlugin(pluginTOC, { slugify });
}

NOTE: We use plugin options to make sure both plugins use an identical slugify function, this works OK for most headings, but will break if a heading is repeated.

To use the table of contents in your templates, apply the toc shortcode to your template content:

[!IMPORTANT] The first matched heading on the page should be the topmost. Don't put an <h3> before an <h2>!

<aside>
  {% toc %}
    {{ content | safe }}
  {% endtoc %}
</aside>
<article>
  {{ content }}
</article>

Configuring

You can override some of the options at the time that you call it, or all of them when you add it in your Eleventy config. All of the options will be merged together, with the options passed to the filter taking precedence over the options passed to the plugin (which take precedence over the defaults).

Override the defaults for your whole site (defaults are shown):

{
    tags: ["h2", "h3", "h4"], // tags (heading levels) to include
    ignoredHeadings: ["[data-toc-exclude]"], // headings to ignore (list of selectors)
    ignoredElements: [], // elements (within the headings) to ignore when generating the TOC (list of selectors)
    ul: false, // whether to a use a `ul` or `ol`
    inheritAttributes: false, // whether to inherit heading attributes to TOC links
    wrapper: function (toc) {
        // wrapper around the generated TOC
        return `<nav class="toc">${toc}</nav>`;
    },
}

Or override as it's being invoked:

<aside>
  {% toc(tags=['h2', 'h3']) %}
    {{ content }}
  {% endtoc %}
</aside>

If you have specific headings which you don't want to be included in the TOC, you can add one of the ignoredHeadings selectors to exclude these headings (defaults to the[data-toc-exclude] selector).

Set inheritAttributes to true to copy heading attributes to TOC links, or provide an array of attribute names (e.g. ['data-value']) to copy only specific ones.

License

MIT