-
Notifications
You must be signed in to change notification settings - Fork 63
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
Fix start line numbers being highlighted #181
Fix start line numbers being highlighted #181
Conversation
✅ Deploy Preview for rehype-pretty-code canceled.
|
src/index.ts
Outdated
@@ -353,7 +353,7 @@ export default function rehypePrettyCode( | |||
|
|||
const lineNumbers: number[] = []; | |||
if (meta) { | |||
const matches = meta.matchAll(/\{(.*?)\}/g); | |||
const matches = meta.matchAll(/(?<!\S)\{(.*?)\}/g); |
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.
Does Safari support this? I remember we had to use a trick to avoid negative lookbehinds to make it work on old Safari 😓
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.
Interesting, I didn't even think this would be running client-side. According to https://caniuse.com/js-regexp-lookbehind, it's been supported since Safari 16.4. That's also backed up by Safari's release notes.
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.
If you're concerned about how recent that is, I could probably figure out another way?
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.
Depends how you're using it, but it can be used on either/both server and client yes. Safari 16.4 is fairly recent..
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.
Just pushed a new regex that avoids negative lookbehind by surrounding the pattern in \B
(matches any non-word-boundary character). Strictly speaking only the leading \B
is necessary to avoid picking up showLineNumbers{number}
, but I figure you wouldn't want to match some theoretical {number}word
meta property, either.
This PR fixes an issue where combining
showLineNumbers{number}
with line highlighting would cause the start line number to be highlighted when it shouldn't be.Included is an expanded test fixture that demonstrates the issue, which can be fixed by using a negative lookbehind in the regex used to find highlighted line numbers in the meta.
Before this PR:
After this PR: