Coder Social home page Coder Social logo

toc-generator's Introduction

TOC-Generator

A simple script that uses the headings in the content of the page and automatically generates a table of contents with styling.

You can see an example image of a generated TOC in this example image.


Setup and Usage

First, install the toc-generator.js file.

Then edit the parts shown below:

// "page-contents" is where the generator will get the header elements.
var pageContents = document.getElementById("page-contents");

// "toc" is where the generator will put the table of contents.
document.getElementById("toc").appendChild(toclist);

By default, the generator uses h2, h3, and h4 elements while generating the TOC and reserves h1 element for the page title. But if you want to include h1 elements, just edit this variable in the toc-generator.js file to be true.

var includeh1 = false;

Finally, you can add this line right before the </body> tag in your HTML file.

<script src="/path/to/toc-generator.js"></script>

Styling

If you don't want to deal with the styling, you can just install the toc-style.css file and import it into your main css file.

@import url("/path/to/toc-style.css");

Or you can add it directly to your HTML file without importing it into your main css file by putting this line between <head> tags.

<link rel="stylesheet" href="/path/to/toc-style.css" />

Custom Styling

You can change main list's appearance by adding styling to the container where the script put the list (#toc as default). Container and sublists have these classes as default:

#toc { /* your styling */ }
#toc ul { /* your styling */ }
#toc ul li { /* your styling */ }
#toc ul li a{ /* your styling */ }

.toc-sublist { /* for h2 */ }
.toc-doublesublist { /* for h3 */ }
.toc-triplesublist { /* for h4 */ }

When you exclude h1 tags, all the sublists go up one heading level.

.toc-sublist { /* for h3 */ }
.toc-doublesublist { /* for h4 */ }
.toc-triplesublist { /* this class is not used when h1 is not included  */ }

If you change these classes, don't forget to also update those classes' name in the toc-generator.js file.

toc-generator's People

Contributors

caganseyrek 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.