Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table Of Contents: Include headings exclusively within the core/post-content block #69366

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

yogeshbhutkar
Copy link
Contributor

@yogeshbhutkar yogeshbhutkar commented Feb 28, 2025

What?

Closes #69359

This PR updates the logic to ensure the Table of Contents block only considers Heading elements within the core/post-content block. Previously, it included all headings on the page, including those in the Header and Footer, which could lead to unintended results.

Why?

Including Heading elements from outside the core/post-content block—such as those in the Header and Footer—in the Table of Contents block is not ideal, as it should strictly focus on the main content. The block should serve its intended purpose of summarizing the structure of the post by tabulating only the Heading tags used within the post content.

How?

The previous implementation used getClientIdsWithDescendants to get the client IDs of all the blocks in the editor. When the Show Template mode is toggled ( template-locked editing mode ), this selector literally returns all the blocks within the editor. That includes the Header, Footer, and the blocks within the Template. This behavior causes the bug mentioned above.

The current PR updates the usage to use getBlockOrder, which uses a recursive approach to exclusively get the nested blocks within the post-content using its Client ID. If the Client ID becomes undefined for some reason ( e.g. when editing mode is set to post-only ), in such a scenario, getBlockOrder gracefully falls back to return the top-level blocks.

Testing Instructions

  1. Add Heading elements to the Header, Footer, or within the Template.
  2. Create a new page.
  3. Insert the Table of Contents (TOC) block.
  4. Add Heading elements inside the post content.
  5. Verify that the TOC block only includes headings from the post content and excludes those from the Header, Footer, or Template.
  6. Toggle Show Template and confirm that the behavior remains consistent.

Screenshots

Before After
before after

Testing edge cases
recursion-test

@yogeshbhutkar yogeshbhutkar marked this pull request as ready for review February 28, 2025 07:31
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: ryelle <ryelle@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@shail-mehta shail-mehta added [Block] Table of contents (experimental) Affects the Table of contents Block [Type] Bug An existing feature does not function as intended labels Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Table of contents (experimental) Affects the Table of contents Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Table of Contents: Includes headings outside of post content in template view
2 participants