-
Notifications
You must be signed in to change notification settings - Fork 509
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-generate IDs for all <dt> elements #5190
Auto-generate IDs for all <dt> elements #5190
Conversation
This change causes all <dt> elements in rendered HTML output to have ID values auto-generated based on their text content — in the same way all heading values in rendered HTML output have auto-generated IDs. Related: mdn/content#12165 (comment)
Hey @sideshowbarker! Thanks for getting this started. We definitely want to do this and this already looks great. The only thing I would ask is to duplicate the function( KumaScript is undergoing an overhaul and I would prefer we not depend on this function from KumaScript. Thanks a lot! |
Generating IDs for In fact of course we have to modify the heading-id-generation as well, since heading IDs and <h2>A thing</h2>
<dl>
<dt>A thing</dt>
<dd>...</dd>
</dl> (This also means that generating IDs for I'm really unfamiliar with the Yari code (outside KumaScript) but it looks as if section IDs get injected in So I'm not sure duplicating slugify is the way to go. It might be better to adapt |
We’ve moved this handling to the injectSectionIDs() code. This reverts commit e23d622.
Indeed. I turns out to be better in pretty much every possible way. So I’ve now changed the patch here to add the handling in In hindsight, I should have done my homework right and figured out myself that
My initial rewrite here doesn’t yet go so far as doing that. I’m happy to try to update it to do that — if we agree that we do in fact really want to make that be the behavior.
It’d be more complicated for sure. How much more complicated, I don’t yet — because I haven’t looked at the code in detail. But the issue with it is that the IDs are generated in document order, and after the the current code generates an ID, I think it doesn’t keep state information about the association between that ID and the element it belongs to. So, if it generates an ID for some If the existing code there has access to the entire DOM for the document at that point, I think it’d be relatively uncomplicated to implement what I outlined above. But if that code doesn’t actually have access to the DOM for the whole document, then it would not be able to look up that
Yeah, it might be easier to first test for any breakage of existing live-sample IDs. |
@wbamberg, looks like you and @sideshowbarker has pretty much got this covered. I am going to approve the PR but, I wil leave merging to you when you both feel this is ready. |
Wow, thanks Mike for coming up with a fix! I will take a look.
Having thought about this a bit more, it's unlikely to be a real problem. As far as I can tell it would need a page where:
Given also that most live sample headings are going to contain spaces (since they are a description of an example) and dt IDs aren't, (by your algorithm in this patch) it's even less likely to happen. |
OK, yeah — hadn’t thought about it in those terms, but indeed that all makes sense. So it seems like the patch in this PR may be good to go — OK with you if I go ahead and merge it? |
Please give me a bit of time to look at it properly first. Sorry to be slow, but I want to be as sure as I can be, and I'm not at all familiar with it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried this out and overall it seems to work pretty great. I had a couple of comments - the uniquify at least looks worth addressing, but the other one might not have a better solution.
I would love to have review from someone familiar with this code though, but I'm not sure who that would be.
This seems to successfully uniquify headings and
=> <h2 id="im_a_thing"><a href="#im_a_thing" title="Permalink to I'm a thing">I'm a thing</a></h2>
<div>
<dl>
<dt id="im_a_thing_2">i'm a thing</dt>
<dd>
<p>also a thing</p>
</dd>
<dt id="im_a_thing_3">I'm a thing</dt>
<dd>
<p>also a thing</p>
</dd>
<dt id="im_a_thing_4">Im a thing</dt>
<dd>
<p>also ...</p>
</dd>
</dl>
</div>
<h2 id="im_a_thing_5"><a href="#im_a_thing_5" title="Permalink to i'm a thing">i'm a thing</a></h2> Also we're getting much nicer ID values for But, this regresses sidebars: I believe this is because it changes the case of the ID in It's pretty hairy stuff :(. |
Yup — thanks for catching that. I’ve pushed an update with a fix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @sideshowbarker for the PR and @wbamberg for the great review. 🚢
@sideshowbarker I see the following error with the build:
|
...includes fixing the lowercasing we added (the code added earlier wasn’t actually assigning the lowercased string back to the id variable.)
Tested a fix and just now pushed an update with the change |
This changes takes some headings for examples that have live samples and that are of this form: > ### multiply ... and changes them to this form: > ### Example using "multiply" That ensures those headings get more-descriptive and more-helpful anchors/fragments of the form foo#example_using_multiply — and it also ensures that the live-sample behavior for those won’t break after mdn/yari#5190 lands, and all dt elements have generated IDs (in which case that foo#multiply anchor will take readers to the actual definition for the `multiply` term, rather than to an example).
This changes takes some headings for examples that have live samples and that are of this form: > ### multiply ... and changes them to this form: > ### Example using "multiply" That ensures those headings get more-descriptive and more-helpful anchors/fragments of the form foo#example_using_multiply — and it also ensures that the live-sample behavior for those won’t break after mdn/yari#5190 lands, and all dt elements have generated IDs (in which case that foo#multiply anchor will take readers to the actual definition for the `multiply` term, rather than to an example).
This changes takes some headings for examples that have live samples and that are of this form: > ### multiply ... and changes them to this form: > ### Example using "multiply" That ensures those headings get more-descriptive and more-helpful anchors/fragments of the form foo#example_using_multiply — and it also ensures that the live-sample behavior for those won’t break after mdn/yari#5190 lands, and all dt elements have generated IDs (in which case that foo#multiply anchor will take readers to the actual definition for the `multiply` term, rather than to an example).
testing/content/files/en-us/learn/css/css_layout/introduction/flex otherwise fails without this change, with the following error: > error: MacroLiveSampleError within > /home/runner/work/yari/yari/testing/content/files/en-us/learn/css/css_layout/introduction/flex/index.html, > line 20 column 4 (unable to find any live code samples for "flex_1" > within /en-US/docs/Learn/CSS/CSS_layout/Introduction/Flex) And we need make sure not to touch the IDs for class=bc-data divs, because the system does case-sensitive matching on those.
OK, after making a few other fixes, CI is all green now ✅ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @sideshowbarker for sticking this one out. Much appreciated. 🎉
Thanks for you help and guidance — and @wbamberg too |
This change causes all
dt
elements in rendered HTML output from Markdown source to have ID values auto-generated based on their text content — in the same way all heading values in rendered HTML output have auto-generated IDs.Related: mdn/content#12165 (comment) cc @wbamberg
This is an initial naïve proof-of-concept attempt at implementing ID generation for
dt
output — I’d be happy to further iterate on this patch as much as needed.I’d also be happy to write up an additional patch for generating IDs in output from HTML sources (in addition to Markdown sources), if somebody can point me to where I’d need to patch — and if we think it’s actually important enough to have this for the HTML-source case, given that we have almost no HTML sources left in the repo, except in the
mozilla/projects/nss
andtools
subtrees.And I’m also happy to write accompanying tests.