How to build a TOC block?

Created on 12 February 2022, over 3 years ago
Updated 30 June 2025, 28 days ago

Problem/Motivation

Surprised this isn't already part of this module but figured easy enough to create. Unfortunately i started by extending the existing TocBlockBase; which has errors with all of the classes it is implementing; so that was a bad place to start.

After i simply extended the core BlockBase this seemed to work. Going from the _view hook code in the README i see i can pass TOC and/or a modified version of my content (used to create the TOC).

This raises questions:

- of course i don't want to add a TOC block with my rewritten content into the layout of my content; then i end up with 2 copies of my content.
- but if i remove adding the content, the anchor links in the TOC don't go anywhere.

How is this expected to be done?

I see 2 options:
1. I need a fake duplicate view mode which is used to simply create the TOC and then the TOC block is placed in the real full_content view mode without any of the content used in the fake one. I am sure this will become problematic as the layout becomes more complex. Not to mention it is redundant.
2. seems like far more reasonable - i write extra js to addin anchor IDs to all the heading tags.

Option 2 sounds promising but wondering why that isn't at least an option here.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Active

Version

2.0

Component

Code

Created by

🇨🇦Canada liquidcms

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024