Skip to content

nicolasartman/generate-table-of-contents

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

generate-table-of-contents

A simple no-dependency script that generates a table of contents structure from a DOM element.

Requirements

No other libraries are required, but it uses element.querySelectorAll so the browser must support that.

Usage

If you're running in a plain browser environment, it will add itself to window as the function generateTableOfContents. If you're bundling commonjs modules, it exports a single function with the same signature.

generateTableOfContents(DOMElement, options)

Options

  • startLevel: an integer specifying the heading level to start the table at. For example, generateTableOfContents(element, {startLevel:3}) will find all h3s through h6s under element and return a table of contents element with up to four levels (for headers 3, 4, 5, and 6).
  • endLevel: an integer specifying the heading level to start the table at (values over 6 are ignored).

Tests

npm test to run all the tests.

The tests use emmet's abbreviation format to cut down on manual html typing for input and expected output generation.

About

A simple standalone script that generates a table of contents structure from a DOM element

Resources

License

Stars

Watchers

Forks

Packages

No packages published