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

html-table-of-contents

v0.1.1

Published

Generates a table of contents for your HTML document based on the headings present.

Downloads

6

Readme

#html-table-of-contents

Generates a table of contents for your HTML document based on the headings present.

Where do I get it?

You can download it from github at https://github.com/matthewkastor/html-table-of-contents or, if you have node installed you can get it from npm

npm install html-table-of-contents

Usage

Using this module in your browser is as simple as including it in your page and calling htmlTableOfContents() after the page has loaded.

The table of contents will be generated in the first element with the id of toc. It will consist of a series of sibling div's whose class directly maps to the heading level of the heading it describes. By default there is no styling done to the table of contents, to allow you to style it however you wish. This module comes with a css stylesheet which you can include in your page if you would like to. The provided stylesheet simply indents entries in the table of contents based on the heading level in the document. Take a look at the stylesheet html-table-of-contents.css, in this module's root folder, to get an idea of how to access the table of contents entries if you wish to create your own stylesheet.

If you're using this module outside of a browser you will have to supply a reference to a dom document object, unless you've called it document and have declared your document object globally. I did not require any specific module for parsing the DOM, because there are a few out there and it would be rude of me to force you to use a specific one for such a simple function.

Generating a table of contents in Browser

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>
            html-table-of-contents Example
        </title>
        <script src="./node_modules/html-table-of-contents/src/html-table-of-contents.js"
              type="text/javascript">
</script>
        <link rel="stylesheet"
            type="text/css"
            href="./node_modules/html-table-of-contents/html-table-of-contents.css" />
    </head>
    <body onload="htmlTableOfContents();">
        <p>
            <b>Contents</b>
        </p>

        <div id="toc">
        </div>

        <h1>
            top heading 1
        </h1>

        <h2>
            second heading
        </h2>

        <h1>
            top heading 2
        </h1>
    </body>
</html>

Generating a table of contents in Node

// parse your html into a DOM Document using jsdom
// or something https://npmjs.org/package/jsdom

var fs = require('fs'); 
var jsdom = require("jsdom").jsdom;
var htmlTableOfContents = require('html-table-of-contents');

var html = fs.readFileSync('example.html', 'utf8');
// javascript written for the browser expects global
// window and document objects
var document = global.document = jsdom(html, null, {
    features: {
        FetchExternalResources : false,
        ProcessExternalResources : false
    }
});
var window = global.window = global.document.parentWindow;

htmlTableOfContents();
// alternatively, if you've called the 
// document object something other than 
// document, you may supply it as the 
// first argument and everything will 
// work out fine.
// htmlTableOfContents(nonstandardDocumentReference);

console.log(document.documentElement.innerHTML);

// Shazam. The document now contains a table of 
// contents you didn't have to write.