Code Monkey home page Code Monkey logo

jquery-toc's Introduction

A plugin that generates table of contents(TOC) automatically for both jQuery and non-jQuery.

Install

bower install jquery-toc

Compatibility

  1. non-jQuery version works on IE9+
  2. jQuery version works on anywhere with jQuery

Showcase

https://idiotwu.me/understanding-es6-promise/

Usage

non-jQuery

// returns an ordered-list of headers in page content
var toc = initTOC(options);

// then you can append it to anywhere you like
elem.appendChild(toc);

jQuery

// will append TOC list to target element
$(elem).initTOC(options);

Options

initTOC({
    selector: 'h1, h2, h3, h4, h5, h6',
    scope: 'body',
    overwrite: false,
    prefix: 'toc'
});
  • String:selector: headers selector, default is 'h1, h2, h3, h4, h5, h6'.
  • String:scope: selector to specify elements search scope, default is 'body'.
  • Boolean:overwrite: whether to overwrite existed headers' id, default is false, which means plugin will preserve the existed id property and create a string like toc-1 for those don't have id assigned.
  • String:prefix: string to prepend to id/href property, default is 'toc', which generates a string like toc-0. Set to true to generate an automatic prefix based on the title.

Example

source:

<aside id="toc"></aside>

<article id="post">
    <h1>this will be ignored due to specific selector</h1>
    <p>...</p>

    <h2>this is a h2</h2>
    <p>...</p>

    <h3>this is a h3</h3>
    <p>...</p>

    <h2>yet another h2</h2>
    <p>...</p>

    <h3>and another h3</h3>
    <h4 id="wow">existed id will be preserved due to options</h4>
</article>

with jQuery version:

$('#toc').initTOC({
    selector: 'h2, h3, h4',
    scope: '#post',
    overwrite: false,
    prefix: 'toc'
});

non-jQuery version:

var container = document.querySelector('#toc');

var toc = initTOC({
    selector: 'h2, h3, h4',
    scope: '#post',
    overwrite: false,
    prefix: 'toc'
});

container.appendChild(toc);

yield:

<aside id="toc">
    <ol>
        <li>
            <a href="#toc-0">this is a h2</a>
            <ol>
                <li>
                    <a href="#toc-1">this is a h3</a>
                </li>
            </ol>
        </li>
        <li>
            <a href="#toc-2">yet another h2</a>
            <ol>
                <li>
                    <a href="#toc-3">and another h3</a>
                    <ol>
                        <li><a href="#wow">existed id will be preserved due to options</a></li>
                    </ol>
                </li>
            </ol>
        </li>
    </ol>
</aside>

<article id="post">
    <h1>this will be ignored due to specific selector</h1>
    <p>...</p>

    <h2 id="toc-0">this is a h2</h2>
    <p>...</p>

    <h3 id="toc-1">this is a h3</h3>
    <p>...</p>

    <h2 id="toc-2">yet another h2</h2>
    <p>...</p>

    <h3 id="toc-3">and another h3</h3>
    <h4 id="wow">existed id will be preserved due to options</h4>
</article>

License

MIT.

jquery-toc's People

Contributors

elgandoz avatar idiotwu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.