Skip to content

Commit

Permalink
fix: ignore highlighting for no lang (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks authored Dec 19, 2023
1 parent 744ad8a commit 643ea3a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ export default function rehypePrettyCode(
defaultCodeBlockLang,
);

if (!lang) return;

const lineNumbers: number[] = [];
if (meta) {
const matches = meta.matchAll(/\{(.*?)\}/g);
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const runFixture = async (fixture, fixtureName, getHighlighter) => {
const html = await getHTML(code, {
keepBackground: !resultHTMLName.includes('keepBackground'),
defaultLang: (() => {
if (testName === 'no-highlighting') {
return undefined;
}

const lang = testName.split('.')[1];
if (!lang) {
return undefined;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/no-highlighting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```
test
```
10 changes: 2 additions & 8 deletions test/results/defaultLang.inline=js.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,8 @@
</style>
<h2>Default language</h2>
<p>{ inline: 'js' }</p>
<figure data-rehype-pretty-code-figure="">
<pre
style="background-color: #24292e; color: #e1e4e8"
tabindex="0"
data-language=""
data-theme="github-dark"
><code data-language="" data-theme="github-dark" style="display: grid;"><span data-line=""><span>const x = true;</span></span></code></pre>
</figure>
<pre><code>const x = true;
</code></pre>
<p>
<span data-rehype-pretty-code-figure=""
><code
Expand Down
55 changes: 55 additions & 0 deletions test/results/no-highlighting.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

<style>
html {
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
}
body {
margin: 30px auto;
max-width: 800px;
}
pre {
padding: 16px;
}
span > code {
background: black;
padding: 4px;
}
[data-highlighted-line], [data-highlighted-chars] {
background-color: rgba(255, 255, 255, 0.25);
}
code[data-line-numbers] {
counter-reset: line;
}
code[data-line-numbers]>[data-line]::before {
counter-increment: line;
content: counter(line);
display: inline-block;
width: 1rem;
margin-right: 2rem;
text-align: right;
color: gray;
}

[data-rehype-pretty-code-figure] code[data-theme*=' '],
[data-rehype-pretty-code-figure] code[data-theme*=' '] span {
color: var(--shiki-light) !important;
background-color: var(--shiki-light-bg) !important;
}

@media (prefers-color-scheme: dark) {
[data-rehype-pretty-code-figure] code[data-theme*=' '],
[data-rehype-pretty-code-figure] code[data-theme*=' '] span {
color: var(--shiki-dark) !important;
background-color: var(--shiki-dark-bg) !important;
}
}

.diff.add {
background-color: rgba(0, 255, 100, 0.25);
}
.diff.remove {
background-color: rgba(255, 100, 200, 0.35);
}
</style>
<pre><code>test
</code></pre>

0 comments on commit 643ea3a

Please sign in to comment.