Skip to content

Commit

Permalink
fix: line class omission (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom authored Oct 21, 2023
1 parent 75e5a90 commit 159ddfc
Show file tree
Hide file tree
Showing 29 changed files with 76 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ function toFragment(
const code = pre.children[0];

// Remove class="shiki"
pre.properties.className = undefined;
if (Array.isArray(pre.properties?.className) && pre.properties?.className.includes('shiki')) {
const className = pre.properties.className.filter(c => c !== 'shiki');
pre.properties.className = className.length > 0 ? className : undefined;
}

if (!keepBackground) {
pre.properties = {};
pre.properties.style = undefined;
}

pre.properties['data-language'] = lang;
Expand Down Expand Up @@ -396,7 +399,8 @@ export default function rehypePrettyCode(
element.children = [{ type: 'text', value: ' ' }];
}

element.properties.className = undefined;
const className = element.properties.className.filter(c => c !== 'line');
element.properties.className = className.length > 0 ? className : undefined;
element.properties['data-line'] = '';
onVisitLine?.(element as LineElement);

Expand Down
1 change: 1 addition & 0 deletions test/results/ansi.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ <h2>ANSI</h2>
vite --port 3123
</div>
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="ansi"
Expand Down
3 changes: 3 additions & 0 deletions test/results/caption.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Caption</h2>
<p>caption="Immutable variable declaration"</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -59,6 +60,7 @@ <h2>Caption</h2>
./components/index.js
</div>
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -76,6 +78,7 @@ <h2>Caption</h2>
<p>caption="Immutable variable declaration" /caption/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
1 change: 1 addition & 0 deletions test/results/defaultLang.block=js.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Default language</h2>
<p>{ block: 'js' }</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
1 change: 1 addition & 0 deletions test/results/defaultLang.inline=js&block=js.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Default language</h2>
<p>{ block: 'js', inline: 'js' }</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
1 change: 1 addition & 0 deletions test/results/defaultLang.inline=js.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Default language</h2>
<p>{ inline: 'js' }</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language=""
Expand Down
1 change: 1 addition & 0 deletions test/results/defaultLang.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Default language</h2>
<p>js</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
1 change: 1 addition & 0 deletions test/results/diff.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<h2>Diff</h2>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="diff"
Expand Down
1 change: 1 addition & 0 deletions test/results/filterMetaString.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<h1>Filter meta string</h1>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
1 change: 1 addition & 0 deletions test/results/highlightChars.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Highlight chars</h2>
<p>/carrot/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
5 changes: 5 additions & 0 deletions test/results/highlightCharsById.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Highlight chars by id</h2>
<p>/age/#a /name/#a /setAge/#b /setName/#b /42/#c /'Taylor'/#c</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down Expand Up @@ -62,6 +63,7 @@ <h2>Highlight chars by id</h2>
<p><code>/&#x3C;div/#a /id="true">/#b</code></p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -77,6 +79,7 @@ <h2>Highlight chars by id</h2>
<p>/age/#id</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -92,6 +95,7 @@ <h2>Highlight chars by id</h2>
<p>/age/2</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -101,6 +105,7 @@ <h2>Highlight chars by id</h2>
<p>/car/1</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
18 changes: 18 additions & 0 deletions test/results/highlightCharsComplex.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h1>Highlight chars complex</h1>
<p>/&#x3C;span class="test/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -43,6 +44,7 @@ <h1>Highlight chars complex</h1>
<p>/span class="test"/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -52,6 +54,7 @@ <h1>Highlight chars complex</h1>
<p>/test"/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -61,6 +64,7 @@ <h1>Highlight chars complex</h1>
<p>/"test/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -70,6 +74,7 @@ <h1>Highlight chars complex</h1>
<p>/"te/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -79,6 +84,7 @@ <h1>Highlight chars complex</h1>
<p>/st"/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -88,6 +94,7 @@ <h1>Highlight chars complex</h1>
<p>/st">/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -97,6 +104,7 @@ <h1>Highlight chars complex</h1>
<p>/="te/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -106,6 +114,7 @@ <h1>Highlight chars complex</h1>
<p>/>/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -115,6 +124,7 @@ <h1>Highlight chars complex</h1>
<p>/&#x3C;/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -124,6 +134,7 @@ <h1>Highlight chars complex</h1>
<p>/&#x3C;>/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="html"
Expand All @@ -133,6 +144,7 @@ <h1>Highlight chars complex</h1>
<p>/theme: 'monokai'/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -144,6 +156,7 @@ <h1>Highlight chars complex</h1>
<p>/ theme: /</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -155,6 +168,7 @@ <h1>Highlight chars complex</h1>
<p>/light &#x26;&#x26; fluffy/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -166,6 +180,7 @@ <h1>Highlight chars complex</h1>
<p>/car/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -176,6 +191,7 @@ <h1>Highlight chars complex</h1>
<p>/car,/2</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -186,6 +202,7 @@ <h1>Highlight chars complex</h1>
<p>/,car/1</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -196,6 +213,7 @@ <h1>Highlight chars complex</h1>
<p>/ot,ca/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
3 changes: 3 additions & 0 deletions test/results/highlightCharsRange.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Highlighted chars range</h2>
<p>/getStringLength/1-2</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -47,6 +48,7 @@ <h2>Highlighted chars range</h2>
<p>/getStringLength/1,3</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -60,6 +62,7 @@ <h2>Highlighted chars range</h2>
<p>/getStringLength/2,5,6</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
1 change: 1 addition & 0 deletions test/results/highlightMultipleChars.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Highlighted multiple chars</h2>
<p>/carrot/ /return/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
4 changes: 4 additions & 0 deletions test/results/highlightPartialNode.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Highlighted partial node</h2>
<p>/carrot/</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -44,6 +45,7 @@ <h2>Highlighted partial node</h2>
</div>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -54,6 +56,7 @@ <h2>Highlighted partial node</h2>
</div>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -64,6 +67,7 @@ <h2>Highlighted partial node</h2>
</div>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
1 change: 1 addition & 0 deletions test/results/highlightedLine.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Highlighted line</h2>
<p>{1}</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
1 change: 1 addition & 0 deletions test/results/highlightedLines.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Highlighted lines</h2>
<p>{1, 3, 6-8}</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
2 changes: 2 additions & 0 deletions test/results/highlightedLinesWithShowLineNumbersAt.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Highlighted lines with showLineNumbersAt</h2>
<p>{1, 3, 6-8} showLineNumbers{3}</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand All @@ -51,6 +52,7 @@ <h2>Highlighted lines with showLineNumbersAt</h2>
<p>showLineNumbers{3} {1, 3, 6-8}</p>
<div data-rehype-pretty-code-fragment="">
<pre
class="github-dark"
style="background-color: #24292e"
tabindex="0"
data-language="js"
Expand Down
Loading

0 comments on commit 159ddfc

Please sign in to comment.