0.1.3 • Published 4 months ago

eleventy-plugin-toc-util v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

eleventy-plugin-toc-util

npm

Usage

eleventy.config.js:

import * as toc from "eleventy-plugin-toc-util";

export default async function (config) {
  // Use https://www.11ty.dev/docs/plugins/id-attribute/ to add ids to
  // all headings in the site

  // Or create filter to add ids and anchors to individual contents
  config.addFilter("attachId", (html) => toc.attachId(html));

  // Only add anchors to h2 and h3
  config.addFilter("attachAnchor", (html) => toc.attachIdAnchor(html, "h2,h3"));

  config.addFilter("toc", (html) => toc.createToc(html));
}

Template file:

{% extends "base.njk" %}
{% block content %}
  <article>
    <!--Add ids and anchor to content-->
    {% set content = content | attachId | attachAnchor %}

    <aside>
      <!--Render table of content-->
      {{ content | toc | safe }}
    </aside>

    {{ content | safe }}
  </article>
{% endblock %}

See function JSDocs in toc-util.js for more information.

Similar solutions

Tasks

  • TOC title is based on headings id, so special characters are excluded. Think of a way to create TOC without using heading ids
0.1.3

4 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.1.0

5 months ago