Improvement: A user should be able to exclude headings from the ToC block in the editor.
What problem does this address?
In the ToC block, there is currently no way to exclude a specific heading from the list. I'm aware of two workarounds, but neither is good, and they both have drawbacks:
- Using a span or non-heading element to render a heading. This is not semantically correct and could also impact SEO
- Converting the ToC to a static list. This presents another issue, e.g., if new headings are added or the IDs change, the ToC will be out of sync.
One use case would be very long articles, meaning a long ToC. However, I can also see this feature being useful for cases where a heading is not "ToC worthy" but we still want to retain the article's semantic hierarchy.
What is your proposed solution?
I've outlined a solution in #69063, but after discussing it briefly with @swissspidy, I realized it seems a bit out of scope for the current PR. Also, I think more discussion on the specific UI could be helpful.
My solution relies on a class assigned to the heading, which is simple but also opaque if you don't read the code.
I will remove this functionality from #69063 to align it with the scope of #51605 and open a new PR with my suggestion to exclude headings to cover the issue you're reading now.
This would be a nice addition. A lot of Table of Content blocks include this feature:
- https://wpbeaveraddons.com/docs/powerpack/modules/table-of-contents/exclude-headings-toc/
- https://rankmath.com/kb/table-of-contents-block/#exclude-headings
- https://powerpackelements.com/docs/include-exclude-headings/
Re-posting my original suggestion for visibility:
Introduce a proper toggle on the heading block that the TOC block could then use. I would also take a look at how Google Docs does this for example. You can remove a heading from the TOC simply by clicking on an "X" next to it.
There's no point in mis-using CSS class names for this.
Hi @gmovr
- I have raised a PR for the feature : https://github.com/WordPress/gutenberg/pull/70235
- This is the first iteration and will require more feedback in order to proceed
Let me know your thoughts on this and how we can proceed with the next steps. Thanks
Thank you so much for your contribution, @Vrishabhsk; however, this has already been merged into core.
https://github.com/WordPress/gutenberg/pull/69063
Hi @mrfoxtalbot 👋
- As per my evaluation of the PR #69063, this particular feature was excluded
- This was the commit : Remove: toc-exclude class functionality. Out of scope.
- This feature was also striked-out in the PR description
- Also i was not able to find the feature/ability in core/gutenberg latest branches to exclude certain headings.
Let me know if im mistaken. Thanks
Looping in @gmovr as well.
Hi @Vrishabhsk. This issue was opened as part of a conversation I had with @gmovr, and my understanding was that the PR he wrote addresses the issue, but I could be wrong. I will let @gmovr reply.
Also i was not able to find the feature/ability in core/Gutenberg latest branches to exclude certain headings.
FWIW, I just tested this and the feature is available if you install the plugin: https://playground.wordpress.net/?plugin=gutenberg
Hi @mrfoxtalbot, thanks for the response.
- The feature that is live is for showing headings upto a certain level i.e. till h1-h6.
- This issue suggests that users aren't able to exclude a singular/multiple headings regardless of their level.
- So if i wanted to hide only one of h3 headings in my ToC block, im unable to do so.
- The PR raised by me allows the user to do so
I see what you mean: @gmovr's PR introduces an option to exclude headings based on their depth but your PR provides more detailed control, right?
I think there's merit to the idea, but it might be too specific to include in core. Given that this specific issue is covered by @gmovr's PR, I would suggest opening a separate GitHub issue and linking your PR to it, to avoid confusion and allow for a separate discussion.
I hope that helps.
Hi @mrfoxtalbot yes you are correct, its for more control over specific headings.
Instead of creating a new issue, IMO this issue was branched out from the PR for the sole purpose of giving more control over the headings whereas the PR was originally meant for https://github.com/WordPress/gutenberg/issues/51605.
We can reopen this issue itself and also reopen the PR that was previously closed (if not possible to reopen the PR, i can raise it again).
Let me know your thoughts on this. Thanks
I see what you mean. Let me reopen this and summon @gmovr.