-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Handle titles starting with numbers #13
Comments
Hey Chad!
Starting an id attribute value with a number is valid according to HTML. It’s fine.
Yep, and it does so to prevent DOM clobbering, as a safety measure. I’m open to adding an option here to prefix things. |
Thanks for the detailed response @wooorm!
I think the two solutions you provided resolve the need for this option. Hopefully this issue can help someone in the future! Thanks again :) |
This comment has been minimized.
This comment has been minimized.
Cool, glad those alternatives work for you! |
Initial checklist
Problem
Note: Not sure if this is better solved here or in github-slugger. I decided to open here since I think a solution to this problem would make that package stray from it's intention of being as close to GitHub's slugger as possible. Happy to move over to that repo if you think it's a better fit there though.
I have a scenario where an author of some content has started the header with a number. This results in an
id
that starts with a number, which isn't valid according to the CSS spec (HTML spec was a little less clear whether it was valid or not). Regardless, when I tried to reference the header usingdocument.querySelector
, it throws an error saying I'm using an invalid selector.Solution
A solution I could take on my end would be to append something to the beginning of these IDs and update the links, but I thought it'd be better to be done upstream (either in
rehype-slug
orgithub-slugger
).I created a quick solution for
github-slugger
where it just appends an underscore (_
) to any id that'd start with a number. I figured I should reach out first before submitting a PR though. GitHub appendsuser-content-
to all of the ids of in the rendered markdown. You can see this when inspecting the README of this project.This is where I'm unsure of where this solution should be implemented. GitHub doesn't include the
user-content-
in the url produced for each header, but instead handles that client side. Sogithub-slugger
only sluggifies what comes after theuser-content-
. So if we start appending something like an underscore, it means that package may deviate too much from its goal.For those reasons, I think it may be the responsibility of this package.
Alternatives
I touch on the alternatives in the Solutions section above, but I'll reiterate and summarize here:
rehype-slug
as I think this could be a problem for many people (not just me)github-slugger
if it doesn't force that project to deviate from it's goal of "emulate the way GitHub handles generating markdown heading anchors as close as possible."The text was updated successfully, but these errors were encountered: