Code Monkey home page Code Monkey logo

toc-generator's Introduction

TOC-Generator

What is it

A simple, ready to use javascript to add auto generated, super customizable Table Of Content to any page element. You can see an example of what it does at https://alf3run.github.io/TOC-Generator.

Features

  1. Ready to use: no need to write javascript code, only html is required;
  2. Your style: add your own style with CSS just the way you're used to do;
  3. No 3rd party dependencies: it's written in plain javascript and css so you don't need to import any other libraries such as jQuery, etc;
  4. Modular javascript code: you can choose the feature you want to include by deleting or commenting the relative functions.

How It Works

The TOC-Generator make use of data-toc attribute to determine its parent element. Then, it select all the <h(n)> tag inside and insert a hierarchically ordered list made out of them to the parent's top. Every tabel of content generated this way has a default id="toc-(nth)", where (nth) is an integer corresponding to the nth generated table, and a default class="toc-default".

The toc.css contains some basic styling rules to make the TOC-Generator ready to use but you can customize it the way you like by modifying the file, overriding the .toc-default class in an external css, or adding your own class using the TOC-Generator Options below.

How To Use

Just put the js and css folders or their content inside your project root direcotry, then import the toc.css and toc.js in your .html file. Now the TOC-Generator is ready to use.

In your .html file chose the element you want the table of content generated for and add the attribute data-toc to it. For example:

<div data-toc>
  <h2>Title 1</h2>
  <h3>Title 2</h3>
  <h2>Title 3</h2>
  <!-- more content -->
</div>

Options

You can modify some aspects of the generated tabel of content by adding the data- attribute below to the same element where data-toc is:

  1. data-toc-id - overrides the default toc generated id if setted to a not empty value;
  2. data-toc-class - allows to add any custom class to your generated table of content. It does not override the default .toc-default class;
  3. data-toc-header - overrides the default toc header with a user defined one. If set to none (not case sensitive!) it will remove the header;
  4. data-toc-parent-id - allows the user to set the element the toc will be appended to by specifying its id.

toc-generator's People

Contributors

alf3run avatar

Stargazers

 avatar

Watchers

James Cloos avatar  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.