All Projects → jdsteinbach → eleventy-plugin-toc

jdsteinbach / eleventy-plugin-toc

Licence: other
11ty plugin to generate a TOC from page content

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to eleventy-plugin-toc

eleventy-plugin-prismic
Eleventy plugin and shortcodes to fetch and present Prismic content
Stars: ✭ 39 (-13.33%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-upgrade-help
Helper plugin when upgrading your Eleventy project to a new major version.
Stars: ✭ 33 (-26.67%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-plugin-cloudinary
An Eleventy shortcode that allows you to add an image from your cloudinary account
Stars: ✭ 28 (-37.78%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-navigation
A plugin for creating hierarchical navigation in Eleventy projects. Supports breadcrumbs too!
Stars: ✭ 88 (+95.56%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-plugin-metagen
An Eleventy shortcode that generates metadata for Open Graph, Twitter card, generic meta tags and a canonical link.
Stars: ✭ 28 (-37.78%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-plugin-pwa
An eleventy plugin to generate service-worker for PWA. Powered by Google Workbox
Stars: ✭ 46 (+2.22%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-plugin-embed-tweet
A plugin for embedding tweets on the server side during build time
Stars: ✭ 24 (-46.67%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-load
Resolve dependencies and post-process files in your Eleventy project
Stars: ✭ 28 (-37.78%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-plugin-social-images
Create dynamic images sized for social media tags for your Eleventy content.
Stars: ✭ 35 (-22.22%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-plugin-inclusive-language
A linter plugin to check for inclusive language in markdown files.
Stars: ✭ 32 (-28.89%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-plugin-svelte
Eleventy plugin to support svelte templates
Stars: ✭ 40 (-11.11%)
Mutual labels:  eleventy, eleventy-plugin
eleventy-plugin-unfurl
Unfurl links into rich cards, as seen in places like Slack and Twitter
Stars: ✭ 37 (-17.78%)
Mutual labels:  eleventy, eleventy-plugin
demo-eleventy-serverless
Run Eleventy in a serverless function
Stars: ✭ 53 (+17.78%)
Mutual labels:  eleventy
frontenso-11ty-starter
Production-ready 11ty+Gulp+Webpack Starter that features Nunjucks, SASS, TailwindCSS (with JIT complier), and ESNext.
Stars: ✭ 24 (-46.67%)
Mutual labels:  eleventy
frontend-tips
Super tiny, quick tips, tricks and best practices of front-end development
Stars: ✭ 511 (+1035.56%)
Mutual labels:  eleventy
11ty-blog-starter
11ty v1.0, Tailwind v3. Works when JS is disabled
Stars: ✭ 55 (+22.22%)
Mutual labels:  eleventy
11r
America's favorite Eleventy blog template.
Stars: ✭ 135 (+200%)
Mutual labels:  eleventy
fernfolio-11ty-template
The super simple portfolio template built with Eleventy and Netlify CMS
Stars: ✭ 64 (+42.22%)
Mutual labels:  eleventy
bymattlee-11ty-starter
A starter boilerplate powered by 11ty, Sanity, Gulp, Tailwind CSS, rollup.js, Alpine.js and Highway.
Stars: ✭ 27 (-40%)
Mutual labels:  eleventy
pokemon-icons
Fan art inspired by Pokémon
Stars: ✭ 85 (+88.89%)
Mutual labels:  eleventy

eleventy-plugin-toc

This Eleventy plugin will generate a TOC from page content using an Eleventy filter.

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`/`ul`
  wrapperClass: 'toc',      // class for the element around the root `ol`/`ul`
  ul: false,                // if to use `ul` instead of `ol`
  flat: false,              // if subheadings should appear as child of parent or as a sibling
}

Usage

1. Install the plugin

npm i --save eleventy-plugin-toc

2. Make sure your headings have anchor IDs

Your heading elements must have ids before this plugin will create a TOC. If there aren't ids on your headings, there will be no anchors for this plugin to link to.

I use markdown-it-anchor to add those ids to the headings: Eleventy config example

// .eleventy.js

const markdownIt = require('markdown-it')
const markdownItAnchor = require('markdown-it-anchor')

module.exports = eleventyConfig => {
  // Markdown
  eleventyConfig.setLibrary(
    'md',
    markdownIt().use(markdownItAnchor)
  )
  // ... your other Eleventy config options
}

3. Add this plugin to your Eleventy config

// .eleventy.js

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

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

3.1 You can override the default plugin options

module.exports = function (eleventyConfig) {
  eleventyConfig.addPlugin(pluginTOC, {
    tags: ['h2', 'h3'],
    wrapper: 'div'
  })
}

4. Use the filter in your layout template(s)

Because Eleventy only provides the content variable to layout templates (not to content files), you'll need to put this markup in a layout template:

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

If you're using Nunjucks, include the safe filter:

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

If you want to conditionally render a wrapper element, the filter will return undefined when no markup is generated:

{% if content | toc %}
  <aside>
    {{ content | toc }}
  </aside>
{% endif %}

5. 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"}' }}
</aside>

Options

Name Default Value Type Purpose
tags ['h2', 'h3', 'h4'] array of strings which heading tags are used to generate the table of contents
wrapper 'nav' string tag of element wrapping toc lists; '' removes wrapper element
wrapperClass 'toc' string class on element wrapping toc lists
wrapperLabel undefined string aria-label on element wrapping toc lists
ul false boolean lists are ul if true, ol if false
flat false boolean use flat list if true; use nested lists if false

Roadmap

  • Some tests would be nice
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].