1.0.2 • Published 3 years ago

toc-generate v1.0.2

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

TOC Generate

| Create a table of contents for a HTML, ESModule

https://nodei.co/npm/toc-generate.png?downloads=true&downloadRank=true&stars=true

Demo

Demo Link

Usage

Script HTML

  1. Include one of the link in to HTML
<script src="https://dunggramer.github.io/toc-generate/dist/toc-generate.js" defer></script>
<script src="https://gitcdn.xyz/cdn/DungGramer/toc-generate/master/dist/toc-generate.js" defer></script>
  1. Call the function
 <script>
    document.addEventListener('DOMContentLoaded', function() {
        const tableOfContent = tocGenerate({
            contentWrapperSelector: "main",
            headingLevelFrom: 2,
            viewablePercentToHighlight: 70,
            showsHighLight: true,
            showsParentHighlight: true,
        });

        const tocSelector = document.querySelector("#toc");
        tocSelector.appendChild(tableOfContent);
    });
</script>

ESModule

npm i toc-generate
# or 
yarn add toc-generate

You may not use tocSelector if the content is not already displayed.

import tocGenerate from 'toc-generate';

const tableOfContent = tocGenerate({
    contentWrapperSelector: "main",
    headingLevelFrom: 2,
    viewablePercentToHighlight: 70,
    showsHighLight: true,
    showsParentHighlight: true,
});

const tocSelector = document.querySelector("#toc");
tocSelector.appendChild(tableOfContent);

API

ParameterTypeDefaultDescription
contentWrapperSelectorstring'body'The selector for the content wrapper
tocSelectorstringundefinedThe selector for the TOC
headingLevelFromnumber2The heading level to start from
headingLevelTonumber6The heading level to stop at
showsHighLightbooleanfalseWhether to show the highlight
showsParentHighlightbooleanfalseWhether to keep the parent highlight, only works when showsHighLight is true
viewablePercentToHighlightnumber70The viewable percent to highlight