Add a Plugin

Default Options

{
tags: ['h2', 'h3', 'h4'], // which heading tags are selected
// headings must each have an ID attribute
wrapper: 'nav', // element to put around the root `ol`
wrapperClass: 'toc' // class for the element around the root `ol`
}

Usage

1. Install the plugin

npm i --save eleventy-plugin-toc

2. Add the plugin to Eleventy config

Note: you'll need to make sure you have ids on heading elements before this plugin will create a TOC. If there aren't ids, there will be nothing for links in this TOC to link to. (I recommend using markdown-it-anchor to add those ids to the headings: Eleventy config example)

// .eleventy.js

const pluginTOC = require('eleventy-plugin-toc')

module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(pluginTOC)
}

3. Use the filter in your template

<article>
{{ content }}
</article>
<aside>
{{ content | toc }}
</article>

4. Override default options if necessary

Pass a stringified JSON object (must be JSON.parse()-able) as an option for in your template. Because this is an object, you only need to include the key-value pairs you need to override; defaults will be preserved.

<aside>
{{ content | toc: '{"tags":["h2","h3"],"wrapper":"div","wrapperClass":"content-tableau"}' }}
</article>