-
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
Auto fill HTML Anchor (IDs) #6182
Comments
There was some previous discussion about something similar in #2394 (comment). It looks like there was some consensus but that we decided to punt on it. I'm into the idea. My only reservation is that we'd have to figure out a way to avoid duplicate IDs from being accidentally inserted into the page. |
This would help for #1760, too. |
No strong opinion against it, and it seems like a valid use case. We just want to think through the edgecases and complexity it might bring. Do we look to make sure a user doesn't use the same ID twice? What if an auto-ID conflicts with an existing ID used in the HTML code? If we only suggest an ID to auto-fill, does it really add any value compared to doing nothing at all? Could we do with a button that says something like "Copy heading" or something? |
Prefixing the IDs with the post slug would make it reasonably unique, particularly if the pattern is continued into the customisation focus. (Sidebars could be prefixed with the sidebar slug, etc.) Also, we'd want to only auto-fill it once, then the author can modify it if they need to. |
I would love this. Requiring converting the block into custom HTML is not a nice experience towards this end. A duplicate checker shouldn't be too hard, and a default ID setting of a slug of the header text (plus a number if there are multiple with the same text) hopefully isn't too complex to implement |
This would be great, I'm trying to implement something like this now but also with the ability to copy the anchor for sharing using clipboard.js First step is getting headings to auto-generate an ID, right now my solution would be using PHP preg functions. I thought of extending the gutenberg blocks, anchor attribute, to auto-fill with the heading title, but my skill level isn't there yet to make those kind of customizations. For the duplicate ID issue, you can handle it like WordPress does with Permalinks by appending a sequenced number to the end of the ID, i.e., id="page", id="page-2" Another option could require user intervention, a warning that there is duplicate ID's present with fix suggestions? |
Is this feature making any movement? I personally would love this feature. I have a pattern of building pages using 'sections' at the top level of the page blocks which then use innerBlocks to customize the layout/look of the section content. The ability to generate a unique ID for these top-level block anchors would be a great convenience. I often need to build a navigation of some sort that has scroll link anchors somewhere on the site and I can't count on the client to remember to input an ID for each section, not to mention making it unique across the whole page. |
Will mention this issue in the weekly editor slack meeting.. |
@LukaszJaro @ZebulanStanphill It may be helpful for Table of Contents #21234 |
@mahnunchik, that is my current use case, need it for table of contents. Would be a very nice addition to have a core table of contents block with pre-filled anchors. Also the heading level checker #22650 is a good idea, would be useful for something similar for anchors, check if duplicate anchor is present. |
It is pretty awesome! I'm looking forward to have it merged 😃 |
Happy to move this forward, but the details need to be figured out: how to name the ids to avoid issues with duplicates; handling updating the content of a heading, which is tricky, because it could break existing links, etc. |
To avoid duplicate IDs due to x2 Heading blocks with the same text, could we check the document for these duplicates and if found then append an integer to the end of the duplicates' anchor? eg:
I'd say we mirror the permalink behaviour for Posts, which I believe is to leave the reference intact when the title changes. We could apply the same for Heading anchors because as you say to do otherwise could break existing hyperlinks out there in the wild. |
Might also be worth looking at how GitHub handles this, as they auto-link headers in readmes. |
Here's a update from the slack discussion, basically it's very complicated! Zeb:That seems like a feature for the Heading block, not the Table of Contents block. Auto-adding anchors to other blocks from another block would be a bit of a weird behavior. (In fact, an earlier implementation of the Table of Contents block did just this, but it caused all sorts of issues with undo/redo.) Zeb:computer: 10:29 AM youknowriad 10:31 AM Zeb:computer: 10:31 AM youknowriad 10:32 AM matias 10:32 AM Zeb:computer: 10:33 AM michaelarestad:yolo: 10:34 AM Zeb:computer: 10:35 AM matias 10:35 AM |
The Table of Contents block ended up being merged without any dedicated headings API. It seems the obvious next step is to implement automatic id generation at the Heading block. Here's my suggestion for how it should work:
How does this sound? Once we determine how this feature should behave, it shouldn't be difficult to implement in a PR. |
I've some ideas about generated anchor. If we use text from the header itself when it would be more user friendly to have I know and use in my project the It allows to slugify not only latin strings but cyrillic, arabian or even emoji. In this case I will not use "Custom HTML anchor" at all because almost all anchors will be human understandable. |
I think we should fetch heading in run time in the backend as well as front-end if we don't want to fetch only core headings. We can search heading present on page using And then we can auto-generated HTML anchor (generated from the heading ) like this var TOC = ''; let header_array = $( 'div.entry-content' ).find('h1, h2, h3, h4, h5, h6' ) header_array.each( function (index,value){
}); Like this. |
Closing in favor of #29561 which is more up to date and has an attached pull request. |
Feature request/suggestion
In #1734 (#2394 PR) the ability to configure the HTML Anchor of a heading was added. I'd propose to auto-fill this value so users don't have to manually configure these. I think only auto-filling them, but still allowing edits would be preferable.
The text was updated successfully, but these errors were encountered: