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: ignore highlighting for no lang #140

Merged
merged 2 commits into from
Dec 19, 2023
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: 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>