0.1.4 • Published 11 months ago

html-toc-generator v0.1.4

Weekly downloads
-
License
MIT
Repository
-
Last release
11 months ago

Table of Contents Generator

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: 1.33 kB (minified and gzipped)

Installation

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

npm install html-toc-generator

Usage

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

  1. Import the library:
import TableOfContents from 'html-toc-generator';
  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();

Options

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

OptionTypeDefault ValueOptionalDescription
attributesstring[]['h1', 'h2', 'h3']YesAn array of HTML heading tag names to include in the table of contents.
appendTostring'body'YesThe selector of the element to which the table of contents should be appended.
containerClassNamestring'toc'YesThe CSS class name to be applied to the container element of the table of contents.
orderedbooleanfalseYesA boolean value indicating whether the generated table of contents should be ordered or unordered.
contentSelectorstring'#content'YesThe selector of the element containing the content to generate the table of contents from.

s

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.

0.1.4

11 months ago

0.1.3

11 months ago