Repository navigation
Conversation
|
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 Unlinked AccountsThe 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. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: -363 B (0%) Total Size: 7.67 MB 📦 View Changed
|
|
Flaky tests detected in 24fdcb1. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/18449311717
|
0eea33c to
329c4f0
Compare
| function block_core_table_of_contents_add_full_urls( $html ) { | ||
| global $wp; | ||
| if ( isset( $wp->request ) ) { | ||
| $current_url = home_url( $wp->request ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
Thanks @scruffian. I think I'd prefer to only process the headings on the server, and just save |
t-hamano
left a comment
There was a problem hiding this comment.
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
| const headings = useSelect( | ||
| ( select ) => { | ||
| return getLatestHeadings( select, clientId ); | ||
| }, | ||
| [ clientId ] | ||
| ); |
There was a problem hiding this comment.
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)
...
| if ( $post ) { | ||
| $content = $post->post_content; | ||
| } | ||
| } elseif ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
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. |
|
Thanks for taking a look, @Mamaduka!
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
left a comment
There was a problem hiding this comment.
Personally, I think the ToC block should be removed from this conditional statement:
gutenberg/packages/block-library/src/heading/edit.js
Lines 51 to 53 in 525f643
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:
|
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. |
| 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'] ); |
There was a problem hiding this comment.
I think this should always be the permalink.
There was a problem hiding this comment.
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 ); |
There was a problem hiding this comment.
Why are we using a cache? Is this for really large pages?
We already had clear direction from Matias on this (#29560 (comment)) |
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? |
Co-authored-by: Ben Dwyer <ben@scruffian.com>
Co-authored-by: Ben Dwyer <ben@scruffian.com>
Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>
24fdcb1 to
fd3f166
Compare
|
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. |
|
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. |
|
We can close this now that #80318 is merged. |
|
Props to @mikachan for initially pushing this forward 🙇 |

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_contentglobal. Also traverses through the available blocks and nested blocks to find all headings.Testing Instructions
In the post context:
In the page context:
In the template context:
Screenshots or screencast
home.html template example from Archeo theme: