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

ah-toc

v0.1.8

Published

The Table of Contents Generator is a JavaScript library that generates a table of contents based on the headings (e.g., `<h1>`, `<h2>`, `<h3>`) within an HTML document. It provides options for customizing the generated table of contents.

Downloads

138

Readme

Table of Contents Generator (+ react hook)

The Table of Contents Generator is a JavaScript library that generates a table of contents based on the headings (e.g., <h1>, <h2>, <h3>) within an HTML document. It provides options for customizing the generated table of contents.

  • No external dependencies
  • Size: 4.04 kB (minified and gzipped)

Installation

You can install the Table of Contents Generator library using npm:

npm install ah-toc

Usage

To use the Table of Contents Generator in your project, follow these steps:

1- Pure Javascript

  1. Import the library:
import TableOfContents from 'ah-toc';
  1. Create an instance of the TableOfContents class:
const options = {
  attributes: ['h1', 'h2', 'h3'],
  appendTo: 'body',
  containerClassName: 'toc',
  ordered: false,
  contentSelector: '#content'
};

const tableOfContents = new TableOfContents(options);
  1. Call the init method to generate the table of contents:
tableOfContents.init();

2- React Hook

Example

import { useRef } from 'react';
import useTableOfContents from 'table-of-contents-library/hook';

const MyComponent = () => {
  const contentRef = useRef();
  const tocData = useTableOfContents({ contentRef });

  return (
    <div>
      {/* Your content goes here */}
      <div ref={contentRef}>...</div>

      {/* Render the table of contents */}
      {tocData && (
        <ul>
          {tocData.children.map((node) => (
            <li key={node.id}>
              <a href={`#${node.id}`}>{node.content}</a>
            </li>
          ))}
        </ul>
      )}
    </div>
  );
};

useTableOfContents Hook

Parameters
  • contentRef: A MutableRefObject containing a reference to the content element in the HTML document that contains the headers.
Returns
  • node: A NodeType object representing the table of contents hierarchy. It will be undefined initially and will be updated once the table of contents is generated.

Options

The TableOfContents class accepts an optional options object during initialization. The available options are:

| Option | Type | Default Value | Optional | Description | | -------------------- | ---------- | -------------------- | -------- | -------------------------------------------------------------------------------------------------- | | attributes | string[] | ['h1', 'h2', 'h3'] | Yes | An array of HTML heading tag names to include in the table of contents. | | appendTo | string | 'body' | Yes | The selector of the element to which the table of contents should be appended. | | containerClassName | string | 'toc' | Yes | The CSS class name to be applied to the container element of the table of contents. | | ordered | boolean | false | Yes | A boolean value indicating whether the generated table of contents should be ordered or unordered. | | contentSelector | string | '#content' | Yes | The selector of the element containing the content to generate the table of contents from. |

Example

Suppose we have the following HTML content:

<div id="content">
  <h1>Introduction</h1>
  <h2>Section 1</h2>
  <h3>Subsection 1.1</h3>
  <h3>Subsection 1.2</h3>
  <h2>Section 2</h2>
  <h3>Subsection 2.1</h3>
  <h3>Subsection 2.2</h3>
</div>

After calling the init method with the default options, the generated table of contents will be appended to the <body> element and will have the following structure:

<div class="toc">
  <ul>
    <li>
      <a href="#introduction">Introduction</a>
      <ul>
        <li>
          <a href="#section-1">Section 1</a>
          <ul>
            <li><a href="#subsection-1-1">Subsection 1.1</a

></li>
            <li><a href="#subsection-1-2">Subsection 1.2</a></li>
          </ul>
        </li>
        <li>
          <a href="#section-2">Section 2</a>
          <ul>
            <li><a href="#subsection-2-1">Subsection 2.1</a></li>
            <li><a href="#subsection-2-2">Subsection 2.2</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</div>

Please note that no CSS styles are added by default to keep the library simple and customizable. You can apply your own CSS styles by targeting the generated classes and elements.

License

This project is licensed under the MIT License.