• Stars
    star
    531
  • Rank 83,129 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Table of Contents Plugin

TOC npm

TOC is a library that will automatically generate a table of contents for your page.

Features

  • Completely customizable
  • Click to smooth scroll to that spot on the page
  • Automatically highlight the current section
  • Can have multiple on a page

Installation

npm install import '@firstandthird/toc'

Usage

Import required scripts:

import '@firstandthird/toc';
// or
import Toc from '@firstandthird/toc';

Setup HTML:

<div class="toc" data-toc="h1">

<h1 data-toc-title="Heading 1">My heading</h1>

Options

Options are set via custom properties:

Property Value Description
data-toc {string|Element|NodeList} Elements to use as headings
data-toc-container {string|Element|NodeList} Element to find all selectors in
data-toc-offset {string|Element|NodeList} Offset to trigger the next headline
data-toc-title {string} Text to be used as title (add this to headings)

Example

Example HTML:

<!DOCTYPE HTML>
<html>
  <head>
    <title>TOC Example</title>
    <link href="styles.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <div class="toc" data-toc="h1, h2, h3"></div>

    <div id="wrapper">
      <h1 data-toc-title="Custom toc title">Page Title</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
      <h2>Sub Heading</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
      <h2>Sub Heading</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
      <h3 id="last" data-toc-title="Custom subsub-heading">SubSub Heading</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fermentum ligula a augue sollicitudin a tincidunt felis tincidunt. Donec et urna augue, sed consectetur lacus. Maecenas tincidunt volutpat lorem. Suspendisse turpis tellus, sodales ac commodo id, rhoncus vel augue. Vestibulum nisl nibh, rutrum eu bibendum vitae, bibendum et libero. Suspendisse vel odio vitae leo commodo lacinia. Sed non lacinia nulla. Pellentesque faucibus euismod dictum. Suspendisse potenti.</p>
    </div>
    <script src="scripts.js"></script>
  </body>
</html>

Example JavaScript:

import '@firstandthird/toc';

Example CSS:

.toc {
    background: #fefefe;
    width: 200px;
    position: fixed;
    border: 1px solid #ddd;
    color: #333;
}
.toc a {
    color: #333;
}
.toc .toc-h2 {
    margin-left: 10px
}
.toc .toc-h3 {
    margin-left: 20px
}
.toc-visible {
    color: #000;
    font-weight: bold;
}
.toc.right {
    right: 0
}

License

MIT License

Copyright (c) 2018 First+Third

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

routie

a tiny javascript hash router
JavaScript
621
star
2

jquery-builder

Build custom versions of jQuery
JavaScript
347
star
3

layouts

an applescript and alfred extension to manage window layouts
AppleScript
272
star
4

grunt-inline-css

Grunt task for turning an html file with linked css to inline css. Great for emails.
JavaScript
195
star
5

mongroup

Monitor a group of processes with mon
Shell
186
star
6

markx

markdown + code => html
JavaScript
127
star
7

videojs-playLists

VideoJS playlists done right!
JavaScript
88
star
8

MilkMaid

A mac app for Remember The Milk - abandoned, fork away!
JavaScript
75
star
9

node-ga

Server Side Google Analytics for Node
JavaScript
60
star
10

hubsearch

Github search with scoring algorithm and sorting
JavaScript
57
star
11

boots

a twitter bootstrap cli
JavaScript
53
star
12

resistance

A javascript flow library
JavaScript
45
star
13

clientjade

command line tool to compile your jade templates into client side templates for use in the browser
JavaScript
44
star
14

grunt-hash

a grunt plugin for appending hashes to files for cache busting
JavaScript
41
star
15

fidel

A basic javascript controller
JavaScript
36
star
16

querystring

A simple javascript querystring library
JavaScript
31
star
17

clientside

Tool to convert server side (CommonJS) modules to client side (browser) modules
JavaScript
27
star
18

cube-dashboard

A config driven dashboard for cube
JavaScript
24
star
19

grunt-bootstrap

Grunt plugin for generating custom builds of bootstrap
JavaScript
16
star
20

ToodleAir

ToodleDo adobe air desktop app for easily adding tasks
JavaScript
15
star
21

gapserver

A utility to let you use modern web technologies when building your PhoneGap apps.
JavaScript
15
star
22

OWAParser

Objective C parser for Outlook Web Access
Objective-C
12
star
23

fakeson

A node.js library to generate fake/mock json documents for rapid prototyping.
JavaScript
10
star
24

OWANotifier

Mac menu bar app for Outlook Web Mail
Objective-C
8
star
25

geektool

A collection of geektool scripts
Python
7
star
26

loggly-dashboard

A Dashboard for Loggly Stats
JavaScript
6
star
27

TumblrThemeUpdater

Python script to update your tumblr theme from an html file stored locally
Python
5
star
28

taskpaper-dates

Python library to parse dates for taskpaper documents
Python
5
star
29

reloadr

LiveReload CLI tool
JavaScript
5
star
30

data-diff

Javascript library to figure out the changes between json data.
JavaScript
5
star
31

dotfiles

Shell
5
star
32

DLabs

Demand Labs UI Framework
JavaScript
4
star
33

cube-reports

A tool to send daily reports from your cube data
JavaScript
4
star
34

envy-proxy

A reverse proxy for envy containers
Nginx
4
star
35

dist

A node.js cli tool and library to generate development and production versions of javascript libraries.
JavaScript
4
star
36

node-placefinder

A node.js library for Yahoo's Placefinder API
JavaScript
4
star
37

docker-extras

a few docker helpers
Shell
3
star
38

mockdata

mock data library for node.js
JavaScript
3
star
39

diet

Put your images on a diet - image resizer for node.js
JavaScript
3
star
40

SimpleRTM

Mac application for Remember The Milk
Objective-C
2
star
41

masher

Node asset manager
JavaScript
2
star
42

AppUI

library for creating web apps
JavaScript
2
star
43

sprite_generator

Python script to generate css sprites
Python
2
star
44

csscoverage

A node.js app to find all unused css selectors on a page
JavaScript
2
star
45

fit

a jQuery plugin for centering and cropping images
JavaScript
2
star
46

reddit-karma

A simple webapp to track reddit karma for a user
JavaScript
2
star
47

evernote-templates

Templates for Evernote
JavaScript
2
star
48

form-storage

Library to store content from forms in localStorage so if browser is refreshed or back button is hit, the data is saved and restored.
JavaScript
2
star
49

express-bundle

An express library to split up your application into sub applications
JavaScript
2
star
50

stalk

cli tool to run a command when files have been changed
JavaScript
2
star
51

framejax

Async File Upload Library
JavaScript
2
star
52

iStyle

basic css lib for styling iOS apps
JavaScript
1
star
53

adobe-air-test

JavaScript
1
star
54

grunt-bytesize

Grunt plugin to output the filesize and gzip size of files
JavaScript
1
star
55

template

An extremely simple javascript template engine
JavaScript
1
star
56

thrive

A base javascript class that allows for easy inheritance
JavaScript
1
star
57

portfolio

My portfolio site
CSS
1
star
58

aws-curl

A library to perform curls across multiple AWS EC2 boxes
JavaScript
1
star
59

strf

String Formatter
JavaScript
1
star
60

rdio-remote

A web app to remotely control RDIO
JavaScript
1
star
61

grunt-clientside

A grunt plugin for clientside
JavaScript
1
star
62

Things2RTM

Script to export tasks from Things and import them into Remember the Milk
Python
1
star
63

gSendMail

Python Library to send emails via gmail
Python
1
star
64

grunt-concat-bower

A grunt plugin to concat bower dependencies
JavaScript
1
star
65

currie

javascript proxy/curry function
JavaScript
1
star
66

evercreate

Cli to create evernote notes and reminders
JavaScript
1
star
67

simple-guid

A really simple guid generator for node and browser
JavaScript
1
star
68

secure-shell-colors

JavaScript
1
star
69

pyitc

Python iTunes Connect library
Python
1
star
70

InstapaperCount

Chrome Extension to show number of unread items in Instapaper title and header
JavaScript
1
star
71

jgaui

Useful Javascript Scripts
JavaScript
1
star
72

grunt-reloadr

Grunt plugin to run a LiveReload server
JavaScript
1
star
73

angular-reorder

Angular directive to reorder elements with drag and drop
JavaScript
1
star
74

shell

Vim Script
1
star