Skip to content
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

Merged

Conversation

ezzatron
Copy link
Contributor

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:

Screenshot 2024-02-17 at 15 07 02

After this PR:

Screenshot 2024-02-17 at 15 07 16

Copy link

netlify bot commented Feb 17, 2024

Deploy Preview for rehype-pretty-code canceled.

Name Link
🔨 Latest commit 7c25e97
🔍 Latest deploy log https://app.netlify.com/sites/rehype-pretty-code/deploys/65d07e0b1fc9c00008478c6a

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);
Copy link
Collaborator

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 😓

Copy link
Contributor Author

@ezzatron ezzatron Feb 17, 2024

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

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..

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants