Thoughts on adding direct-links to heading tags within article content?
See original GitHub issue@tannerdolby commented on Wed Oct 07 2020
URL(s)
Request type
- Correction or update
Details
Hello, MDN team. I’m not so sure this is an issue or problem but something I wanted to ask your team about.
I originally noticed it on this page Content Categories - MDN but I think it applies to all pages within /docs/Web/
. I wanted to click on the section heading, Palpable content to get a direct link to this section without having to scroll down from Content Categories. I inspected the page and copied the corresponding id selector #Palpable_content
and pasted it to the end of the page URL to provide a “direct-link” to that sections content.
I do recognize the “On this page” section containing direct links to main headings of the page in that unordered list which is very helpful. But for sections with more content that require the user to scroll further down the page, this is where I think potentially having direct links on some of the subheadings in long categories could be useful for people sharing links to specific sections within MDN docs.
Proposed update:
- Create direct links for section headings that are further down from the “On this page” direct links. Allowing for a direct link to that section heading to be produced in the browsers address bar upon clicking the
.direct-link
element nested in the heading for fast internal linking. I used the hashtag character as the anchors value (which could also be an SVG if there is a specific icon wanting to be used) within the direct link screen shot I attached below.
The .direct-link
anchor elements could be fully visible or visually hidden from the screen with good ole visibility: hidden
or potentially a fancier way of visually hiding using absolute positioning and translate.
The .direct-link
element could then become visible upon hovering the heading element and allow for a quick click to update the page URL with a direct link to that specific section heading.
<h3 id="Palpable_content">Palpable content <a class="direct-link" href="https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Palpable_content">#</a></h3>
Screenshot with direct link visible for Palpable content heading: (no styling or margins currently applied)
Let me know your thoughts!
@chrisdavidmills commented on Wed Oct 07 2020
Hi @tannerdolby !
Yes, I agree this would be nice, and in fact we did have this at some point, but I think it was disabled in the “viewer” front-end because of some kind of performance reason. If you look at the “wiki”/“edit” version of the page (https://wiki.developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories), you’ll see the feature active there.
I think it’d be good for us to have this feature sorted out again at some point soon in the final UI, therefore I’m going to move your issue over to the platform (kuma) repo so I can add your voice to the discussion. Thanks for speaking up.
Issue Analytics
- State:
- Created 3 years ago
- Comments:30 (27 by maintainers)
Top GitHub Comments
Before I forget, one realistic input is:
This can’t become
Perhaps the simplest solution is to inject a
_
if the first character of the generated ID starts with a character that isn’t[a-z]
. E.g.See if you can research up the current rules about what a valid ID has to be for a DOM.
I haven’t carefully read your comment yet. But I skimmed it and something that stood out is that it might that the sample input HTML is not realistic. If the input HTML is:
I think nothing should be done. Yes, it’s nuts that the same
id
value is repeated and that’s something we need to deal with, but it’s a story for another day. We have a system called “flaws” which deals with finding things like that.A more realistic input would be:
With an input like that, your output should something like:
I.e. If the
h3
has anid
already, keep it. Don’t touch it. If theh3
texts are repeated, increment the normalized ID.Stay away from the
name
attribute entirely. Pretend it doesn’t exist. As Ryan pointed out, it’s a relic of the past but it was ultimately just there to help set theid
.Does that simplify the task a bit?