Skip to content

Table of Contents: Try dynamic rendering - #72110

Closed
mikachan wants to merge 22 commits into
trunkfrom
update/toc-dynamic-rendering
Closed

mikachan wants to merge 22 commits into
trunkfrom
update/toc-dynamic-rendering

Conversation

@mikachan

@mikachan mikachan commented Oct 6, 2025 •

Copy link
Copy Markdown
Member

What?

Closes #43595
Closes #41173
Closes #41031

Enables the Table of Contents block to work with dynamic rendering in the template context (e.g., when placed in site templates like home.html), allowing it to find and display headings from the entire template.

Why?

Changing the block to use dynamic rendering allows it to add headings when outside of the post content context.

It also helps us fix the issue described in #43595, where the generated anchor links don't work in pages on initial save.

It also fixes the undo history bug described in #41031.

How?

Modifies render_block_core_table_of_contents() to detect whether the block is rendering in post content or template context using the $_wp_current_template_content global. Also traverses through the available blocks and nested blocks to find all headings.

Testing Instructions

In the post context:

  1. Create a new post (or edit an existing one)
  2. Add some headings with some content
  3. Insert a Table of Contents block
  4. Ensure the block renders the correct headings, at the correct levels, with links to each page anchor on the front end and in the editor

In the page context:

  1. Create a new page
  2. Add some headings with some content
  3. Insert a Table of Contents block
  4. Ensure the block renders the correct headings, at the correct levels, with links to each page anchor
  5. Save the page
  6. Make sure the page links are correct on the front end and in the editor

In the template context:

  1. Open an existing template (e.g. Blog Home) or create a new template
  2. Add some headings to the template contents, within other blocks (e.g. Group, Columns)
  3. Insert a Table of Contents block
  4. Ensure the block renders the correct headings, at the correct levels, with links to each page anchor on the front end and in the editor

Screenshots or screencast

home.html template example from Archeo theme:

image

@mikachan mikachan added [Type] Enhancement A suggestion for improvement. [Block] Table of contents (experimental) Affects the Table of contents Block labels Oct 6, 2025
@github-actions

github-actions Bot commented Oct 6, 2025 •

Copy link
Copy Markdown

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @drilichili, @Greatdane.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Unlinked contributors: drilichili, Greatdane.

Co-authored-by: mikachan <mikachan@git.wordpress.org>
Co-authored-by: scruffian <scruffian@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: mahnunchik <mahnunchik@git.wordpress.org>
Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: kevin940726 <kevin940726@git.wordpress.org>
Co-authored-by: richtabor <richtabor@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
Co-authored-by: ribaricplusplus <ribaricplusplus@git.wordpress.org>
Co-authored-by: masteradhoc <masteradhoc@git.wordpress.org>
Co-authored-by: dkjensen <dkjensen@git.wordpress.org>
Co-authored-by: mrfoxtalbot <mrfoxtalbot@git.wordpress.org>
Co-authored-by: burtrw <burtrw@git.wordpress.org>
Co-authored-by: aatanasovdev <aatanasov@git.wordpress.org>
Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: felipevelzani <felipevelzani@git.wordpress.org>
Co-authored-by: Sukhendu2002 <sukhendu2002@git.wordpress.org>
Co-authored-by: ZebulanStanphill <zebulan@git.wordpress.org>

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

@github-actions

github-actions Bot commented Oct 6, 2025 •

Copy link
Copy Markdown

Size Change: -363 B (0%)

Total Size: 7.67 MB

📦 View Changed
Filename Size Change
build/scripts/block-library/index.min.js 331 kB -363 B (-0.11%)

compressed-size-action

@github-actions

github-actions Bot commented Oct 7, 2025 •

Copy link
Copy Markdown

Flaky tests detected in 24fdcb1.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/18449311717
📝 Reported issues:

function block_core_table_of_contents_add_full_urls( $html ) {
global $wp;
if ( isset( $wp->request ) ) {
$current_url = home_url( $wp->request );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this is, what if two posts have the same anchors in? Then you'll probably get linked to the wrong one? What if we just always use the permalink and treat a click on the table of contents link as a link to the post directly, but anchored at the right point?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, yeah I think always using the permalink should work, I'll try it. I haven't tested two posts with the same anchors!

@scruffian scruffian left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we're process the post twice and pulling out the headings in both the client and the frontend. If we're going to do all that processing on the server then we probably shouldn't serialize so much to the block markup - we could just save <!-- wp:table-of-contents /-->.

On the other hand if we want to do serialize the post to the block markup, then we probably shouldn't bother processing the post again on the server.

@mikachan

Copy link
Copy Markdown
Member Author

It seems like we're process the post twice and pulling out the headings in both the client and the frontend. If we're going to do all that processing on the server then we probably shouldn't serialize so much to the block markup - we could just save .

On the other hand if we want to do serialize the post to the block markup, then we probably shouldn't bother processing the post again on the server.

Thanks @scruffian. I think I'd prefer to only process the headings on the server, and just save <!-- wp:table-of-contents /-->. This is because it seems simpler, there's less code, and processing on the server appears to work better. I've attempted this in b9fdaa8, and the block is working the same, but would appreciate any feedback!

@mikachan mikachan mentioned this pull request Oct 10, 2025
8 of 18 tasks
@mikachan
mikachan requested a review from t-hamano October 10, 2025 20:59

@t-hamano t-hamano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I think there are two important points here:

We need to decide whether to support heading elements as well as heading blocks. If we do support heading elements, there seem to be performance concerns. See #29560 (comment):

If we decide to only support the core Heading block, we should probably make an announcement about it. There are several existing popular plugins that add their own heading blocks (e.g. Kadence Blocks and Getwid), and we should probably let them know that what they're doing isn't supported, if we choose to go that route.

cc @mtias @ZebulanStanphill @mcsf

The second point is whether to enable automatic anchor generation in Heading blocks. Automatic anchor generation was enabled by default in the past, but was later changed to opt-in. The current specification automatically enables automatic anchor generation if a ToC block exists. In this case, we may encounter more issues like those reported in #38171.

cc @Mamaduka

Comment on lines +63 to +68
const headings = useSelect(
( select ) => {
return getLatestHeadings( select, clientId );
},
[ clientId ]
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a hook? Something like this:

const headings = useLatestHeadings( clientId );

Wel also need to address the warnings in the browser console:

The `useSelect` hook returns different values when called with the same state and parameters.This can lead to unnecessary re-renders and performance issues if not fixed.Non-equal value keys: 0, 1 Error Component Stack
    at TableOfContentsEdit (edit.js:277:9)
...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I've made this a hook in 6cefdda.

I think I've fixed the console warning in 7f5af86, but I'd appreciate any feedback on the solution.

if ( $post ) {
$content = $post->post_content;
}
} elseif ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does this conditional statement match? As I understand it, this conditional statement will not be true unless a REST API request is sent to /wp/v2/block-renderer/core/table-of-contents.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is only true in the editor context, when we need to preview the block on the server side when in the post editor. I've simplified the logic in a7dbc77.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we need to preview the block on the server side when in the post editor.

Is this block rendered server-side in the editor? This block does not use the ServerSideRender component.

@Mamaduka

Copy link
Copy Markdown
Member

Thanks for the PR, @mikachan!

I think we should focus on one problem at a time; for this PR, that would be #43595.

The #41173 would be hardest to fix, from my experience. It would be tough to get working with the client-side generation method we've at the moment. I would recommend reviewing our previous attempts to fix it and checking the blockers.

@mikachan

Copy link
Copy Markdown
Member Author

Thanks for taking a look, @Mamaduka!

I think we should focus on one problem at a time; for this PR, that would be #43595.

I can focus this PR on only fixing #43595. The reason I linked these three issues was that I thought changing to dynamic rendering would address all these bugs at once, but maybe I'm being overly optimistic 😅 In my testing, these issues did seem to be fixed initially.

@t-hamano t-hamano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I think the ToC block should be removed from this conditional statement:

canGenerateAnchors:
!! settings.generateAnchors ||
getGlobalBlockCount( 'core/table-of-contents' ) > 0,

It feels unnatural that the presence or absence of a ToC block affects the behavior of other blocks:

0e4f49b9a568428ecc45dfefdb71a513.mp4

In this PR, the ToC block is dynamic, so there should be no need to actually apply anchors to the heading block. What do you think?

P.S. Although it's not related to this PR, I noticed that block names and anchors overlap:

Image

@t-hamano

Copy link
Copy Markdown
Contributor

Since Beta 1 is just around the corner, I propose to postpone this PR. What do you think?

In my opinion, this block needs further code review and refinement to be ready for shipping.

cc @ryanwelcher @kjnanda

function block_core_table_of_contents_add_full_urls( $html ) {
global $_wp_current_template_content;
if ( ! empty( $_wp_current_template_content ) ) {
$current_url = home_url( $_SERVER['REQUEST_URI'] );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should always be the permalink.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is, if we want it to work on templates as well...

return latestHeadings;
}
// Cache the result and clean up old entries.
headingCache.set( cacheKey, latestHeadings );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using a cache? Is this for really large pages?

@scruffian

Copy link
Copy Markdown
Contributor

We need to decide whether to support heading elements as well as heading blocks. If we do support heading elements, there seem to be performance concerns. See #29560 (comment):

We already had clear direction from Matias on this (#29560 (comment))

@scruffian

Copy link
Copy Markdown
Contributor

The second point is whether to enable automatic anchor generation in Heading blocks. Automatic anchor generation was enabled by default in the past, but was later changed to opt-in. The current specification automatically enables automatic anchor generation if a ToC block exists. In this case, we may encounter more issues like those reported in #38171.

Could an option be to generate IDs if a user hasn't specified them, but if a user wants to override them then we allow it?

@t-hamano

Copy link
Copy Markdown
Contributor

Could an option be to generate IDs if a user hasn't specified them, but if a user wants to override them then we allow it?

Personally, I think that's the best option, and many third-party ToC plugins use this approach. In any case, it's annoying to have to auto-generate anchors for every heading in the editor 😅

image

In that case, we'd need to parse the content and inject anchors into headings server-side, via something like the_content hook. You might be able to use the HTML API.

@getdave
getdave force-pushed the update/toc-dynamic-rendering branch from 24fdcb1 to fd3f166 Compare July 7, 2026 09:30
@github-actions github-actions Bot added the [Package] Block library /packages/block-library label Jul 7, 2026
@t-hamano

Copy link
Copy Markdown
Contributor

The 7.1 Beta1 release is approaching, but I don't think this PR is ready for review yet, so let's punt it to 7.2.

@t-hamano t-hamano moved this from 🔎 Needs Review to 🦵 Punted to 7.2 in WordPress 7.1 Editor Tasks Jul 13, 2026
@getdave

getdave commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

I've tried another approach to this in #80318.

As this PR hasn't received new commits for a while now and Sarah isn't currently available it might be easier to continue work over there.

@Mamaduka

Copy link
Copy Markdown
Member

We can close this now that #80318 is merged.

@Mamaduka Mamaduka closed this Jul 17, 2026
@github-project-automation github-project-automation Bot moved this from 🦵 Punted to 7.2 to ✅ Done in WordPress 7.1 Editor Tasks Jul 17, 2026
@getdave

getdave commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Props to @mikachan for initially pushing this forward 🙇

@Mamaduka
Mamaduka deleted the update/toc-dynamic-rendering branch July 20, 2026 09:54
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 [Package] Block library /packages/block-library [Type] Enhancement A suggestion for improvement.

Projects

No open projects

6 participants