Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/uncenter/eleventy-plugin-toc
🔌 Simple and configurable table of contents generation for Eleventy.
https://github.com/uncenter/eleventy-plugin-toc
eleventy eleventy-plugin
Last synced: 3 days ago
JSON representation
🔌 Simple and configurable table of contents generation for Eleventy.
- Host: GitHub
- URL: https://github.com/uncenter/eleventy-plugin-toc
- Owner: uncenter
- License: mit
- Created: 2023-06-11T03:18:52.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-01T23:43:07.000Z (9 months ago)
- Last Synced: 2024-09-25T20:28:02.498Z (8 days ago)
- Topics: eleventy, eleventy-plugin
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@uncenter/eleventy-plugin-toc
- Size: 133 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @uncenter/eleventy-plugin-toc
> [!NOTE]
> This repository is an updated fork of [JordanShurmer/eleventy-plugin-nesting-toc](https://github.com/JordanShurmer/eleventy-plugin-nesting-toc) with some additional features and bug fixes.Easily generate a table of contents (TOC) for your Eleventy site, with easy configuration and customization.
HTML:
```html
Hello, World
Lorem ipsum dolor sit amet, consectetur adipisicing elit.Greetings from Mars
Lorem ipsum dolor sit amet, consectetur adipisicing elit.The red planet
Lorem ipsum dolor sit amet, consectetur adipisicing elit.Greetings from Pluto
```Generated TOC:
- [Greetings from Mars](#greetings-from-mars)
- [The red planet](#the-red-planet)
- [Greetings from Pluto](#greetings-from-pluto)```html
```
## Install
```sh
npm i @uncenter/eleventy-plugin-toc
pnpm add @uncenter/eleventy-plugin-toc
yarn add @uncenter/eleventy-plugin-toc
bun add @uncenter/eleventy-plugin-toc
```## Usage
Your heading tags will need to have `id`s on them, so that the TOC can provide proper anchor links to them. Eleventy does not do this for you out of the box. You can use a plugin like [markdown-it-anchor](https://www.npmjs.com/package/markdown-it-anchor) to add those `id`s to the headings automatically (or a similar plugin for your Markdown engine of choice).
> [!IMPORTANT]
> Make sure not to duplicate the `module.exports` line in your config file for any of the examples below! If you already have a `module.exports` 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 `markdown-it-anchor` plugin:
```js
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');module.exports = function (eleventyConfig) {
eleventyConfig.setLibrary('md', markdownIt().use(markdownItAnchor));
};
```Then add the TOC plugin:
```js
const pluginTOC = require('@uncenter/eleventy-plugin-toc');module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(pluginTOC);
};
```To use the TOC in your templates, apply the `toc` filter to your template content:
> [!IMPORTANT]
> The first matched heading on the page should be the topmost. _Don't put an `` before an `
`!_
```twig
{{ content | toc | safe }}
{{ content }}
```
### Configuring
You can override some of the [options](#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):
```js
{
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`
wrapper: function (toc) {
// wrapper around the generated TOC
return `${toc}`;
},
}
```Or override as it's being invoked:
```twig
{{ content | toc(tags=['h2', 'h3']) | safe }}
```
If you have specific headings which you don't want to be included in the TOC, you can add one of the `ignoredElements` selectors to exclude these headings (defaults to the`[data-toc-exclude]` selector).
One way to add this attribute is via the use of the [markdown-it-attrs](https://www.npmjs.com/package/markdown-it-attrs) plugin:
```md
## Heading {data-toc-exclude}
```## License
[MIT](LICENSE)