Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: Opensolr Search 4.5.0 Minor update available for module opensolr_search (4.5.0). Release: Block Layout Accordion 1.0.2 Minor update available for module block_layout_accordion (1.0.2). Release: Canvas interactive toggle 1.0.2 Minor update available for module canvas_interactive_toggle (1.0.2). Release: Views HTTP Client 1.0.2 Minor update available for module views_http_client (1.0.2). Release: Block Layout Accordion 1.0.1 Minor update available for module block_layout_accordion (1.0.1). Release: Canvas interactive toggle 1.0.1 Minor update available for module canvas_interactive_toggle (1.0.1). Release: Video.js (HTML5 Video Player) 10.0.0-alpha1 First alpha version released for module videojs (10.0.0-alpha1). Usage Milestone: Simplify Module simplify crossed 10,000 active installs. Release: Audit Trail 1.0.0-alpha7 New alpha version released for module audit_trail (1.0.0-alpha7). Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Toc.js

4,949 sites Security covered Drupal 10–11
View on drupal.org

This module automatically generates a table of contents for your web pages using the TOC.js JavaScript library. It offers flexibility by allowing you to add tables of contents via an extra field on nodes, a dedicated block, or directly within text fields. The generated table of contents supports features like smooth scrolling, highlighting the current section, and customization of selectors and containers.

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+).

Depends on

Dependencies of the latest stable release

  • block Drupal core
  • node Drupal core

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
23
Tracked since
Oct 2024
Latest release
4 months ago
Releases (12 mo)
11 ▼ from 12
Maintenance
Active

Release Timeline

Releases

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