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

alpinejs-table-of-content

v1.0.2

Published

Generate a table of content from headings within an element 📖

Downloads

4

Readme

Alpine JS Table of Content

Generate a table of content from headings within an element 📖

Install

With a CDN

<script
  defer
  src="https://unpkg.com/alpinejs-table-of-content@latest/dist/toc.min.js"
></script>

<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

With a Package Manager

yarn add -D alpinejs-table-of-content

npm install -D alpinejs-table-of-content
import Alpine from 'alpinejs'
import toc from 'alpinejs-table-of-content'

Alpine.plugin(toc)

Alpine.start()

Example

<article
  class="prose mx-auto"
  x-data="{ tableOfContent: [] }"
  x-table-of-content
>
  <h1 id="heading-1">Heading 1</h1>

  <template x-if="tableOfContent.length">
    <ul class="not-prose list-disc">
      <template
        x-for="{ headingId, headingChildren, textContent } in tableOfContent"
      >
        <li>
          <a :href="`#${headingId}`" x-text="textContent"> </a>

          <template x-if="headingChildren.length">
            <ul class="list-disc ml-4">
              <template
                x-for="{ headingId, headingChildren, textContent } in headingChildren"
              >
                <li>
                  <a :href="`#${headingId}`" x-text="textContent"> </a>

                  <template x-if="headingChildren.length">
                    <ul class="list-disc ml-4">
                      <template
                        x-for="{ headingId, headingChildren, textContent } in headingChildren"
                      >
                        <li>
                          <a :href="`#${headingId}`" x-text="textContent"> </a>
                        </li>
                      </template>
                    </ul>
                  </template>
                </li>
              </template>
            </ul>
          </template>
        </li>
      </template>
    </ul>
  </template>

  <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sit, commodi.
  </p>

  <h2 id="heading-2">Heading 2</h2>

  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>

  <h3 id="heading-3">Heading 3</h3>

  <p>Lorem, ipsum dolor.</p>

  <h4 id="heading-4">Heading 4</h4>

  <p>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo sunt alias,
    possimus mollitia nisi pariatur vero numquam at iure labore! Labore est
    laudantium nam voluptates laborum, inventore delectus dolore placeat impedit
    quae?
  </p>

  <h3 id="heading-3.1">Heading 3.1</h3>

  <p>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium velit
    unde, pariatur dolore, eveniet consectetur eligendi tempora dolor nesciunt
    cumque quis repellendus, voluptate perspiciatis eaque quibusdam?
  </p>

  <h2 id="heading-2.1">Heading 2.1</h2>

  <p>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. A, eaque ratione.
    Mollitia placeat vitae voluptas!
  </p>

  <h2 id="heading-2.2">Heading 2.2</h2>

  <p>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique minima
    harum ipsam!
  </p>

  <h3 id="heading-3.2">Heading 3.2</h3>

  <p>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni a est porro
    facilis nam commodi ullam fugiat? Quisquam reprehenderit incidunt sint ad
    facilis ducimus est rerum? Non commodi tempore provident.
  </p>
</article>

All you need is the x-table-of-content directive and a tableOfContent: [] within the x-data of the same element. Exactly as shown in the example.

From there, Alpine JS will read the content within the element with the x-table-of-content directive and locate all the H2, H3 and H4 elements. Generating a nested array of objects which parent and child relationships. Within these objects you'll have:

  • headingId (Comes from the id attribute on the heading - Useful for jump links)
  • headingChildren (If the heading has child headings, these will appear here)
  • textContent (The text content of the heading)

In the example above there is this piece of HTML:

<template x-if="tableOfContent.length">
  <ul class="not-prose list-disc">
    <template
      x-for="{ headingId, headingChildren, textContent } in tableOfContent"
    >
      <li>
        <a :href="`#${headingId}`" x-text="textContent"> </a>

        <template x-if="headingChildren.length">
          <ul class="list-disc ml-4">
            <template
              x-for="{ headingId, headingChildren, textContent } in headingChildren"
            >
              <li>
                <a :href="`#${headingId}`" x-text="textContent"> </a>

                <template x-if="headingChildren.length">
                  <ul class="list-disc ml-4">
                    <template
                      x-for="{ headingId, headingChildren, textContent } in headingChildren"
                    >
                      <li>
                        <a :href="`#${headingId}`" x-text="textContent"> </a>
                      </li>
                    </template>
                  </ul>
                </template>
              </li>
            </template>
          </ul>
        </template>
      </li>
    </template>
  </ul>
</template>

This is looping through the tableOfContent and generating the nested list for the user to use to jump to, or just view the headings within the content.

This example uses Tailwind CSS, this is not required.

Stats