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 🙏

© 2026 – Pkg Stats / Ryan Hefner

tocify.js

v1.0.2

Published

Table of Content Generator

Readme

tocify is a utility that processes HTML content to generate a Table of Contents (TOC) based on the headings (h1, h2, h3, ..., h6) present in the document. It also assigns unique IDs to these headings to ensure proper navigation within the document.

Features

  • Parses raw HTML content and identifies all headings (h1, h2, h3, h4, h5, h6).
  • Automatically assigns unique IDs to headings if not already provided.
  • Generates a Table of Contents (TOC) based on the headings, formatted as a nested list.
  • Returns the modified HTML with assigned IDs and a generated TOC.

Installation

To use tocify.js, you need to have Node.js installed. You can install it via npm.

npm install tocify.js

You also need to install the cheerio package as it is used for parsing and manipulating HTML.

npm install cheerio

Usage

Import the tocify.js function:

const tocify = require("tocify.js");

Example Usage:

const fs = require("fs");
const tocify = require("tocify.js");

const rawHtml = fs.readFileSync("example.html", "utf8");
const { modifiedHtml, tocHtml } = tocify(rawHtml);

// The modifiedHtml contains the HTML with unique IDs for headings
// The tocHtml contains the generated Table of Contents

console.log("Table of Contents:", tocHtml);
console.log("Modified HTML:", modifiedHtml);

Parameters:

  • html (string): The raw HTML content you want to process.

Returns:

  • modifiedHtml (string): The modified HTML with unique IDs for each heading and wrapped in a <div>.
  • tocHtml (string): The HTML string representing the Table of Contents as an unordered list (<ul>).

Example Input:

<h1>Introduction</h1>
<h2>Overview</h2>
<h3>Details</h3>

Example Output:

Table of Contents:

<ul class="table-of-contents">
  <li class="toc-level-1"><a href="#introduction">Introduction</a></li>
  <li class="toc-level-2"><a href="#overview">Overview</a></li>
  <li class="toc-level-3"><a href="#details">Details</a></li>
</ul>

Modified HTML:

<div class="content-wrapper">
  <h1 id="introduction">Introduction</h1>
  <h2 id="overview">Overview</h2>
  <h3 id="details">Details</h3>
</div>

API

tocify(html)

Parameters:

  • html (string): The raw HTML content.

Returns:

An object with the following properties:

  • modifiedHtml: The HTML content with assigned unique IDs for each heading.
  • tocHtml: The generated Table of Contents as an unordered list.

License

MIT License. See the LICENSE file for details.