-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Heading Block: autogenerate anchors #29561
Comments
Previously: #6182 Let's give this a go! Another case to consider that can make the implementation a bit more complex and costly are the possibility of duplicated IDs when they are autogenerated. So we'd need to guard against that. It can get tricky when we are dealing with FSE content, as a heading in a sidebar can have the same name as a heading in the body. If we have too many operations running concurrently we need to be careful to not impact the UX of writing text on a heading block. For example, we might want to commit the anchor highly debounced, or even when the block becomes unselected. If we want to alleviate those cases in the initial roll out, we could make it so that if a "table of contents" blocks is present, then headings auto-generate IDs, otherwise they don't. I think this is worth considering if the implementation gets too tricky. |
Should we even bother trying to generate an id from the heading's content? It would be a lot simpler (and performant) to just have the block generate a (unique) random string of characters upon insertion. I also think that auto-generated anchors that change along with the heading text may be a bad idea, because any existing links to the those anchors won't work anymore. If the id was only set once upon insertion (and of course the user could change it later themselves if they wanted to), the anchor would remain stable across changes to the wording in the heading. |
i think if we do this in the editor as we do in Yoast SEO now you can prevent this. We already know the outline, as the block editor already has that, we can just add a
I think you make some good points, and honestly the only reason I'm against auto generating is very simple: it's not as user friendly. It's nice when I can link to |
@jdevalk Interesting. So how/when does Yoast create the anchors? If I insert a Heading block, when is its anchor generated? |
Yes, for sure, my comment is more towards the fact that all current headings need to be checked, so on a post with 100 headings it can start affecting the typing experience if the anchor is set too often while editing. That's why I mentioned debouncing the operation until after all edits happen. We'd also need to account for table of contents used outside of |
Now that you mention it, that does sound pretty cool. It's definitely possible to implement for the PHP implementation, but I'm not sure about the editor yet. #29394 will take us one step closer, but the next step will be to make the block's PHP implementation work without the |
Auto-changing anchors along with changing heading text sounds like a bad idea. Also I believe user-friendliness should be the priority here. Adding a Auto-generating heading IDs is also very valuable when no TOC block is added to a page. Auto-generating heading IDs would instantly allow search engines to bring their users to the more detailed, more exact part of the solution that is most valuable to the user. Probably in millions of cases over time. |
+1 for idea to auto-generate anchors by default but don't modify it if it was changed manually. For example:
As I mentioned before, I'd like to suggest to use module https://github.com/Trott/slug (or similar) to auto-generate anchors. It allows to slugify not only non-latin languages but also emoji and unicode simbols:
|
I’d also love to be able to get the anchor from the block when using On a related note, does anyone know why the heading level is omitted for H2s (e.g. |
It would be helpful to get some movement on this PR! I look forward to seeing the Table of Contents block included in the Gutenberg plugin. |
What problem does this address?
Heading blocks are significant structural elements in posts, and being able to link to them is a must-have. There is no downside to having an
id
in headings, and they are immensely useful:What is your proposed solution?
This is a proposal to auto-generate anchors for heading blocks.
When the user adds a heading we can generate the anchor from the heading text, so "About Me" gets an anchor
about-me
.The user will still be able to customize their anchor from the block's "Advanced" section if they wish.
What happens if the user edits the heading text?
If the heading was
About Me
with an anchorabout-me
and they change their heading toAbout Us
, then we can detect thatabout-me
matches the auto-generated anchor for theAbout Me
text, and change the anchor toabout-us
. If on the other hand the initial anchor doesn't match (for examplesection-1
) then assume it was customized by the user and leave it alone.What about non-latin languages?
Though not strictly necessary, we could transliterate headings when generating the anchors, just so we get prettier URLs.
The text was updated successfully, but these errors were encountered: