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 🙏

© 2024 – Pkg Stats / Ryan Hefner

table-of-contents-element

v1.0.0

Published

A lightweight zero-dependency Table of Contents generator, distributed as a web component

Downloads

77

Readme

table-of-contents-element

<table-of-contents></table-of-contents>

A lightweight Table of contents generator, distributed as a web component.

Features:

  • Takes all headlines of a webpage or article and turns them into semantically correct nested lists
  • Selection of headlines can be specified by providing the selector attribute, which uses querySelectorAll
  • Handles complex cases: headlines not ordered as expected, but h3 followed by h2 etc.
  • Does NOT use shadow DOM for easy styling and to inherit the style of your page/article; does not come with its own style
  • Adds links to list items, if a headline has an id attribute
  • Can be provided with custom HTML to add a header, footer or wrap the entire list in a <details> tag (see Examples)
  • Zero-dependencies

Usage

Via npm:

npm install table-of-contents-element
import 'table-of-contents-element';

Or as a <script> tag:

<script type="module" src="https://cdn.jsdelivr.net/npm/table-of-contents-element/index.js"></script>

Then use the HTML:

<table-of-contents></table-of-contents>

Including the script automatically defines the custom element.

Examples

Basic usage, finds all h1, h2, h3, h4 of a page:

<table-of-contents></table-of-contents>

Custom headline selector:

<table-of-contents selector="article h2, article h3:not(.newsletter-signup)></table-of-contents>

Or target only headlines that have an id attribute:

<table-of-contents selector="h1[id], h2[id], h3[id], h4[id]"></table-of-contents>

Change list type to ol:

<table-of-contents listtype="ol"></table-of-contents>

Wrap the list in custom HTML:

You can give any child element an attribute called data-toc-render-target to specify where the list items should be rendered. This way, you can wrap the table of contents in a <detail> tag:

<table-of-contents>
    <details>
        <summary>Table of contents</summary>
        <div data-render-target></div>
    </details>
</table-of-contents>

Or add a <header> and <footer> to your table of contents:

<table-of-contents>
    <header>Table of contents</header>
    <div data-toc-render-target></div>
    <footer>powered by table-of-contents-element</footer>
</table-of-contents>

If you do not provide an attribute data-toc-render-target, the generated list will be added as the last child:

<table-of-contents>
    <header>Table of contents</header>
    <!-- toc list will be placed here -->
</table-of-contents>

API

If you need to re-create the table of contents, for example, because the article content changed dynamically, you can call render():

const toc = document.getElementsByTagName('table-of-contents')[0];
toc.render(); // recreates the TOC

The table-of-contents custom element can be created via JS:

const toc = document.createElement('table-of-contents');
toc.setAttribute('selector', 'h2, h3, h4');
document.body.appendChild(toc);

Please note that <table-of-contents> does not observe changes to attributes. In the rare case that you need to change attributes, simply destroy the existing element and re-create it.

Methods

| Method | Description | |:--|:--| | render() | Recreates the table of contents |

Attributes

| Attribute | Values | Default | Description | |:--|:--|:--|:--| | listtype | "ul" or "ol" | "ul" | List type for items | selector | whatever querySelectorAll() accepts | "h1, h2, h3, h4" | Query selector for headlines to generate the TOC from

Properties

None. Note: Attributes are not reflected to properties.