All Projects → tscanlin → Tocbot

tscanlin / Tocbot

Licence: mit
Build a table of contents from headings in an HTML document.

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Tocbot

Remark Toc
plugin to generate a Table of Contents (TOC)
Stars: ✭ 165 (-80.86%)
Mutual labels:  toc
markdown-index
Generate a global index for multiple markdown files recursively
Stars: ✭ 15 (-98.26%)
Mutual labels:  toc
Jekyll Toc
A GitHub Pages compatible Table of Contents generator without a plugin or JavaScript
Stars: ✭ 306 (-64.5%)
Mutual labels:  toc
Github Markdown Toc
Easy TOC creation for GitHub README.md
Stars: ✭ 2,734 (+217.17%)
Mutual labels:  toc
tocdown
A table of contents generator for markdown
Stars: ✭ 11 (-98.72%)
Mutual labels:  toc
PointingToTheMoon
Super slim Jekyll Theme created for math posts with easy mermaid diagram
Stars: ✭ 49 (-94.32%)
Mutual labels:  toc
Editor.md
The open source embeddable online markdown editor (component).
Stars: ✭ 11,741 (+1262.06%)
Mutual labels:  toc
Vim Markdown Toc
A vim 7.4+ plugin to generate table of contents for Markdown files.
Stars: ✭ 427 (-50.46%)
Mutual labels:  toc
vue-showdowns-editor
A markdown editor using codemirror and previewer using @jhuix/showdowns for Vue.js.
Stars: ✭ 27 (-96.87%)
Mutual labels:  toc
Markdown Preview Enhanced
One of the 'BEST' markdown preview extensions for Atom editor!
Stars: ✭ 3,478 (+303.48%)
Mutual labels:  toc
tocer
A command line interface for generating table of contents for Markdown files.
Stars: ✭ 44 (-94.9%)
Mutual labels:  toc
grav-plugin-toc
This plugin automagically generates a (minified) Table of Contents based on special markers in the document and adds it into the resulting HTML document.
Stars: ✭ 12 (-98.61%)
Mutual labels:  toc
angular-progress-bar
This component allow you to easy incorporate progress-bar to angular/ionic project, providing binding and color options
Stars: ✭ 26 (-96.98%)
Mutual labels:  toc
Toc Org
toc-org is an Emacs utility to have an up-to-date table of contents in the org files without exporting (useful primarily for readme files on GitHub)
Stars: ✭ 202 (-76.57%)
Mutual labels:  toc
Github Markdown Toc.go
Easy TOC creation for GitHub README.md (in go)
Stars: ✭ 387 (-55.1%)
Mutual labels:  toc
Markdown Toc
🎄Generate toc for github markdown file.(为 markdown 文件生成 toc 目录)
Stars: ✭ 144 (-83.29%)
Mutual labels:  toc
markedpp
Preprocessor for markdown files
Stars: ✭ 21 (-97.56%)
Mutual labels:  toc
Runoob Pdf
爬取菜鸟教程网站并转PDF__python_crawer_by_chrome
Stars: ✭ 430 (-50.12%)
Mutual labels:  toc
Contents
Table of contents generator.
Stars: ✭ 404 (-53.13%)
Mutual labels:  toc
JekyllTheme-ProjectGaia
Jekyll Theme Project Gaia. V2 Beta Released !! Check it out ->
Stars: ✭ 110 (-87.24%)
Mutual labels:  toc

Tocbot

travis-ci

Tocbot builds a table of contents (TOC) from headings in an HTML document. This is useful for documentation websites or long markdown pages because it makes them easier to navigate. This library was inspired by Tocify, the main difference is that Tocbot uses native DOM methods and avoids the jQuery & jQuery UI dependencies.

Get Started

You can use npm to install it or include the script on the page with HTML.

Download it here

Include JS

Install it with npm.

npm install --save tocbot

OR

Include the script at the bottom of the page before the closing body tag.

<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.11.1/tocbot.min.js"></script>

Include CSS

CSS is used for expanding & collapsing groupings and some basic styling.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.11.1/tocbot.css">

OR

If you installed it with npm and use sass / postcss you might try importing the styles from 'node_modules', see the includePath option documentation for more info

@import 'tocbot/src/scss/tocbot';

Usage

Initialize the script at the bottom of the page before the closing body tag.

tocbot.init({
  // Where to render the table of contents.
  tocSelector: '.js-toc',
  // Where to grab the headings to build the table of contents.
  contentSelector: '.js-toc-content',
  // Which headings to grab inside of the contentSelector element.
  headingSelector: 'h1, h2, h3',
  // For headings inside relative or absolute positioned containers within content.
  hasInnerContainers: true,
});

NOTE: Make sure the body is scrollable and the document headings have id attributes, tocbot and your browser needs these things to make hashes jump to the proper heading, some markdown libraries (like marked) already do this for you.

If content in the div has changed then trigger a refresh (optionally with new options).

tocbot.refresh();

Also you can use it within typescript:

import * as tocbot from 'tocbot';

tocbot.init({
  // Options
});

tocbot.refresh();

tocbot.destroy();

Examples

If you'd like to add your page to this list open a pull request.

Requirements

This library uses vanilla JavaScript. It is less than 350 bytes of CSS and about 3.6Kb of JavaScript (minified and gzipped) it also has no dependencies.

This script works in all modern browsers and IE 9+.

Make sure rendered headings have id attributes, some markdown libraries (like marked) already do this. If you need to do this client side see this script.

NOTE: to exclude anchor elements from smooth scrolling, add the class no-smooth-scroll.

Fixed headers

To handle fixed headers with tocbot, just pass the header offsets as options to tocbot. For example, the options needed for a 40px tall fixed header would be:

tocbot.init({
  headingsOffset: 40,
  scrollSmoothOffset: -40
})

API

Options

// Where to render the table of contents.
tocSelector: '.js-toc',
// Where to grab the headings to build the table of contents.
contentSelector: '.js-toc-content',
// Which headings to grab inside of the contentSelector element.
headingSelector: 'h1, h2, h3',
// Headings that match the ignoreSelector will be skipped.
ignoreSelector: '.js-toc-ignore',
// For headings inside relative or absolute positioned containers within content
hasInnerContainers: false,
// Main class to add to links.
linkClass: 'toc-link',
// Extra classes to add to links.
extraLinkClasses: '',
// Class to add to active links,
// the link corresponding to the top most heading on the page.
activeLinkClass: 'is-active-link',
// Main class to add to lists.
listClass: 'toc-list',
// Extra classes to add to lists.
extraListClasses: '',
// Class that gets added when a list should be collapsed.
isCollapsedClass: 'is-collapsed',
// Class that gets added when a list should be able
// to be collapsed but isn't necessarily collapsed.
collapsibleClass: 'is-collapsible',
// Class to add to list items.
listItemClass: 'toc-list-item',
// Class to add to active list items.
activeListItemClass: 'is-active-li',
// How many heading levels should not be collapsed.
// For example, number 6 will show everything since
// there are only 6 heading levels and number 0 will collapse them all.
// The sections that are hidden will open
// and close as you scroll to headings within them.
collapseDepth: 0,
// Smooth scrolling enabled.
scrollSmooth: true,
// Smooth scroll duration.
scrollSmoothDuration: 420,
// Smooth scroll offset.
scrollSmoothOffset: 0,
// Callback for scroll end.
scrollEndCallback: function (e) {},
// Headings offset between the headings and the top of the document (this is meant for minor adjustments).
headingsOffset: 1,
// Timeout between events firing to make sure it's
// not too rapid (for performance reasons).
throttleTimeout: 50,
// Element to add the positionFixedClass to.
positionFixedSelector: null,
// Fixed position class to add to make sidebar fixed after scrolling
// down past the fixedSidebarOffset.
positionFixedClass: 'is-position-fixed',
// fixedSidebarOffset can be any number but by default is set
// to auto which sets the fixedSidebarOffset to the sidebar
// element's offsetTop from the top of the document on init.
fixedSidebarOffset: 'auto',
// includeHtml can be set to true to include the HTML markup from the
// heading node instead of just including the textContent.
includeHtml: false,
// onclick function to apply to all links in toc. will be called with
// the event as the first parameter, and this can be used to stop,
// propagation, prevent default or perform action
onClick: function (e) {},
// orderedList can be set to false to generate unordered lists (ul)
// instead of ordered lists (ol)
orderedList: true,
// If there is a fixed article scroll container, set to calculate titles' offset
scrollContainer: null,
// prevent ToC DOM rendering if it's already rendered by an external system
skipRendering: false,
// Optional callback to change heading labels.
// For example it can be used to cut down and put ellipses on multiline headings you deem too long.
// Called each time a heading is parsed. Expects a string in return, the modified label to display.
// function (string) => string
headingLabelCallback: false,
// ignore headings that are hidden in DOM
ignoreHiddenElements: false,
// Optional callback to modify properties of parsed headings.
// The heading element is passed in node parameter and information parsed by default parser is provided in obj parameter.
// Function has to return the same or modified obj.
// The heading will be excluded from TOC if nothing is returned.
// function (object, HTMLElement) => object | void
headingObjectCallback: null,
// Set the base path, useful if you use a `base` tag in `head`.
basePath: '',
// Only takes affect when `tocSelector` is scrolling,
// keep the toc scroll position in sync with the content.
disableTocScrollSync: false

Methods

.init

Initialize tocbot with an options object.

tocbot.init(options)

.destroy

Destroy tocbot and remove event listeners.

tocbot.destroy()

.refresh

Refresh tocbot if the document changes and it needs to be rebuilt.

tocbot.refresh()

Troubleshooting

  • Tocbot scrolls to the right position onClick but highlighting doesn't seem to show the active section
    • Try running this from the console: tocbot.refresh({ ...tocbot.options, hasInnerContainers: true }). If that works then one option (hasInnerContainers: true) to handle inner containers should be all you need to add.

Contributing

Contributions and suggestions are welcome! Please feel free to open an issue if you run into a problem or have a feature request. I'll do my best to respond in a timely fashion.

If you want to open a pull request just fork the repo but please make sure all tests and lint pass.

Running Tests

npm run test

Steps to publish

  • Push a branch and open a pull request
  • run npm version <patch|minor|major>
  • Update readme.md with notes
  • Merge the pull request
  • commit dist/
  • run npm publish
  • make release on github

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].