Drupal is a registered trademark of Dries Buytaert

toc_js

Security covered
View on drupal.org

Toc.js module integrate the jQuery plugin TOC.js into Drupal.

TOC.js is a jQuery plugin which automatically generate a table of contents for your page.

The TOC.js jQuery plugin is no longer maintained and hasn't been updated for years. Therefore, we decided to undertake a complete rewrite of the library. The resulting code is now included in version 3.0 and later of this module.

This module provide :

  • An extra field available on node. This extra field display a Toc based on the settings of the node type.
  • A block which can generate a Toc for any kind of page.
  • A text filter allowing to generate a Toc anywhere in a text field.

The Table of content is generated from the front end and allow :

  • Smooth scrolling when jumping to heading
  • Highlight on scroll on the TOC elements
  • Custom configuration for the container and the heading you want to target.

You can put as many TOCs, with specific settings, as you need, on a page.

Requirements

Toc.js module 2.x works with the jquery TOC.js library. The TOC.js library has been totally rewritten for the 3.x version and is now bundled with the module.

Required modules (core module)
* node
* block

Similar modules

  • The TOC API module provide the API to generate a TOC based on configuration entity type. TOC are generate on the backend.
  • The TOC filter module use the TOC API and can generate a TOC for a body field using the a filter on the body.
  • The TOC API Node use the TOC API module and generate a TOC on a whole node, always from the backend.
  • TOC Formatter set a TOC to the top of a text area field. This module uses PHP DOMDocument to manipulate content.
  • ...

See the Comparison of Table of Contents / TOC modules : https://www.drupal.org/node/2278811

Toc.js have a more lightweight approch than theses modules and delegate the most
part of the work to the awesome TOC.js jquery library, which comes with native
options (selectors, containers, smooth scrolling, highlight on scroll).

Installation

* Install as you would normally install a contributed Drupal module. See:
https://www.drupal.org/docs/8/extending-drupal-8/installing-modules
for further information.
* Download the library TOC.js (only for 2.x)
(https://github.com/jgallen23/toc/tree/greenkeeper/update-all)
and place the following file toc.min.js under the libraries/toc folder.
The complete path of the file attended is : libraries/toc/toc.min.js
* Check the status report
* You can use composer to download and install the jQuery library (only for 2.x). For this you have to add to your composer.json file the following repository:

"repositories": [
        {
            "type": "composer",
            "url": "https://packages.drupal.org/8"
        },
		{
            "type": "package",
            "package": {
                "name": "toc/toc",
                "version": "v0.3.2",
                "type": "drupal-library",
                "dist": {
                    "url": "https://raw.githubusercontent.com/jgallen23/toc/greenkeeper/update-all/dist/toc.min.js",
                    "type": "file"
                }
            }
        }
    ],

And "toc/toc": "*" in the require part.

Configuration

For the Toc.js Block

Place a Toc.js block and configure the Toc settings. Toc settings are the same that for node. See below.

For node

  • Enable the Table of content on the content type you want place a TOC
  • Configure the Toc settings
  • In the manage display page configuration of this content type, place the TOC extra field in the region you content to display it, in any view mode you need.

The Toc.js settings available are :

  • Title : the title displayed on top of the Toc list. Default: "Table of contents".
  • Selectors : the elements to use as headings. Each element separated by comma. Default: "h2,h3".
  • Container : Element to find all selectors in. Default: ".node".
  • Prefix : Prefix for anchor tags and class names. Default: "toc".
  • List type : Choose between an unordered HTML list or an ordered HTML list. Default: "ul".
  • Back to top : Add a back to top link on heading. Default: false
  • Back to top label : The back to top link label. Default: 'Back to top'
  • Smooth scrolling : Enable or disable smooth scrolling on click.
  • ScrollTo offset : Offset to trigger with smooth scrolling enabled.
  • Highlight on scroll : Add a class to heading that is currently in focus.
  • Highlight offset : Offset to trigger the next headline.
  • Sticky : Stick the toc on window scroll.
  • Sticky offset : The offset to apply when the toc is sticky.

Table of content per node

This module contains a sub module "Toc.js per node" which permit to enable/disable the Toc per node. Once this sub module enabled, you must allow the override per node for the content type.

You have then for each content type a new settings :

  • Permit to enable/disable toc per node

Once this option checked, you can for each node disable / enable the TOC generated.

See screenshots for the configuration

Theming

  • The Toc generated is not themed. You need to put some css magic for a better look, according to your theme.
  • Relevant class are added on each element item of the TOC list.
  • The scroll and sticky offsets can now be specified using the CSS var(--name, value) format for better responsiveness (version 3.2+).

Activity

Total releases
18
First release
Jan 2025
Latest release
2 days ago
Release cadence
24 days
Stability
50% stable

Release Timeline

Releases

Version Type Release date
3.6.0-beta1 Pre-release Feb 26, 2026
3.5.0 Stable Feb 26, 2026
3.5.0-rc1 Pre-release Jan 26, 2026
3.4.0 Stable Dec 19, 2025
3.4.0-rc1 Pre-release Nov 19, 2025
3.3.1 Stable Nov 11, 2025
3.3.0 Stable Nov 4, 2025
3.3.0-rc1 Pre-release Oct 7, 2025
3.2.2 Stable Sep 13, 2025
3.2.1 Stable Jun 25, 2025
3.2.0 Stable May 27, 2025
3.2.0-rc3 Pre-release May 1, 2025
3.2.0-rc2 Pre-release Apr 30, 2025
3.2.0-rc1 Pre-release Apr 22, 2025
3.x-dev Dev Apr 20, 2025
3.1.1 Stable Mar 8, 2025
3.1.0 Stable Jan 10, 2025
3.1.x-dev Dev Jan 10, 2025