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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export default function rehypePrettyCode(

const lineNumbers: number[] = [];
if (meta) {
const matches = meta.matchAll(/\{(.*?)\}/g);
const matches = meta.matchAll(/\B\{(.*?)\}\B/g);
for (const match of matches) {
if (match[1]) {
lineNumbers.push(...rangeParser(match[1]));
Expand Down
42 changes: 42 additions & 0 deletions test/fixtures/highlightedLinesWithShowLineNumbersAt.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,45 @@ const subtract = (x, y) => x - y;

const multiply = (x, y) => x * y;
```

showLineNumbers{3} {2,4-5}

```js showLineNumbers{3} {2,4-5}
// should not be highlighted
// should be highlighted
// should not be highlighted
// should be highlighted
// should be highlighted
// should not be highlighted
```

{2,4-5} showLineNumbers{3}

```js {2,4-5} showLineNumbers{3}
// should not be highlighted
// should be highlighted
// should not be highlighted
// should be highlighted
// should be highlighted
// should not be highlighted
```

showLineNumbers{3} {2-4}

```js showLineNumbers{3} {2-4}
// should not be highlighted
// should be highlighted
// should be highlighted
// should be highlighted
// should not be highlighted
```

{2-4} showLineNumbers{3}

```js {2-4} showLineNumbers{3}
// should not be highlighted
// should be highlighted
// should be highlighted
// should be highlighted
// should not be highlighted
```
54 changes: 54 additions & 0 deletions test/results/highlightedLinesWithShowLineNumbersAt.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,57 @@ <h2>Highlighted lines with showLineNumbersAt</h2>
<span class="highlighted" data-line="" data-highlighted-line=""> </span>
<span data-line=""><span style="color:#F97583">const</span><span style="color:#B392F0"> multiply</span><span style="color:#F97583"> =</span><span style="color:#E1E4E8"> (</span><span style="color:#FFAB70">x</span><span style="color:#E1E4E8">, </span><span style="color:#FFAB70">y</span><span style="color:#E1E4E8">) </span><span style="color:#F97583">=></span><span style="color:#E1E4E8"> x </span><span style="color:#F97583">*</span><span style="color:#E1E4E8"> y;</span></span></code></pre>
</figure>
<p>showLineNumbers{3} {2,4-5}</p>
<figure data-rehype-pretty-code-figure="">
<pre
style="background-color: #24292e; color: #e1e4e8"
tabindex="0"
data-language="js"
data-theme="github-dark"
><code data-line-numbers="" style="counter-set: line 2;display: grid;" data-language="js" data-theme="github-dark" data-line-numbers-max-digits="1"><span data-line=""><span style="color:#6A737D">// should not be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span data-line=""><span style="color:#6A737D">// should not be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span data-line=""><span style="color:#6A737D">// should not be highlighted</span></span></code></pre>
</figure>
<p>{2,4-5} showLineNumbers{3}</p>
<figure data-rehype-pretty-code-figure="">
<pre
style="background-color: #24292e; color: #e1e4e8"
tabindex="0"
data-language="js"
data-theme="github-dark"
><code data-line-numbers="" style="counter-set: line 2;display: grid;" data-language="js" data-theme="github-dark" data-line-numbers-max-digits="1"><span data-line=""><span style="color:#6A737D">// should not be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span data-line=""><span style="color:#6A737D">// should not be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span data-line=""><span style="color:#6A737D">// should not be highlighted</span></span></code></pre>
</figure>
<p>showLineNumbers{3} {2-4}</p>
<figure data-rehype-pretty-code-figure="">
<pre
style="background-color: #24292e; color: #e1e4e8"
tabindex="0"
data-language="js"
data-theme="github-dark"
><code data-line-numbers="" style="counter-set: line 2;display: grid;" data-language="js" data-theme="github-dark" data-line-numbers-max-digits="1"><span data-line=""><span style="color:#6A737D">// should not be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span data-line=""><span style="color:#6A737D">// should not be highlighted</span></span></code></pre>
</figure>
<p>{2-4} showLineNumbers{3}</p>
<figure data-rehype-pretty-code-figure="">
<pre
style="background-color: #24292e; color: #e1e4e8"
tabindex="0"
data-language="js"
data-theme="github-dark"
><code data-line-numbers="" style="counter-set: line 2;display: grid;" data-language="js" data-theme="github-dark" data-line-numbers-max-digits="1"><span data-line=""><span style="color:#6A737D">// should not be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span class="highlighted" data-line="" data-highlighted-line=""><span style="color:#6A737D">// should be highlighted</span></span>
<span data-line=""><span style="color:#6A737D">// should not be highlighted</span></span></code></pre>
</figure>