1.2.0 • Published 3 years ago

toc-parser v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Generate Table Of Contents HTML

A package for creating table of contents in HTML using vanilla.js See online demo

Installation

npm install toc-parser --save

Usage

Define data-toc on your html.

<div id="table-of-contents"></div>
<div class="content">
  <div data-toc="1">Title 1</div>
  <p>Example paragraph</p>
  <div data-toc="2">Sub Title 1.1</div>
  <p>Example paragraph</p>
  <div data-toc="3">Sub Title 1.1.1</div>
  <p>Example paragraph</p>
  <div data-toc="1">Title 2</div>
  <p>Example paragraph</p>
  <div data-toc="1">Title 3</div>
  <div data-toc="2">Sub Title 3.1</div>
  <div data-toc="3">Sub Title 3.1.1</div>
  <p>Example paragraph</p>
</div>

Initialize the TOCParser

Using CommonJS

const { TOCParser } = require('toc-parser');

Using ES6

import TOCParser from 'toc-parser';
let options = {
  target: "#table-of-contents",
  customClass: {
    ol: 'ol-class',
    li: 'li-class',
    a: 'a-class'
  }
}
let toc = new TOCParser(".content", options);
toc.render();
  • ".content" {String} - The selector of HTML content do you want to generate table of contents
  • options {Object} - Is an object which can contains:
OptionsDescription
targetThe selector of target to placing table of contents
customClassIs an object which can contain the keys ol, li and aThis is useful to customize class on tag ol, li and a
FunctionsDescription
render()Function to render table of contents to specific target
parse()Function to get the HTML string
1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago