From 643ea3afbcf4e280f99638ed7cc55edddd529a48 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 19 Dec 2023 08:54:31 -0800 Subject: [PATCH] fix: ignore highlighting for no lang (#140) --- src/index.ts | 2 + test/fixtures.test.js | 4 ++ test/fixtures/no-highlighting.md | 3 ++ test/results/defaultLang.inline=js.html | 10 +---- test/results/no-highlighting.html | 55 +++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/no-highlighting.md create mode 100644 test/results/no-highlighting.html diff --git a/src/index.ts b/src/index.ts index c1f8aa5..44fde84 100644 --- a/src/index.ts +++ b/src/index.ts @@ -345,6 +345,8 @@ export default function rehypePrettyCode( defaultCodeBlockLang, ); + if (!lang) return; + const lineNumbers: number[] = []; if (meta) { const matches = meta.matchAll(/\{(.*?)\}/g); diff --git a/test/fixtures.test.js b/test/fixtures.test.js index b10ad8e..8fd6b82 100644 --- a/test/fixtures.test.js +++ b/test/fixtures.test.js @@ -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; diff --git a/test/fixtures/no-highlighting.md b/test/fixtures/no-highlighting.md new file mode 100644 index 0000000..59f076b --- /dev/null +++ b/test/fixtures/no-highlighting.md @@ -0,0 +1,3 @@ +``` +test +``` diff --git a/test/results/defaultLang.inline=js.html b/test/results/defaultLang.inline=js.html index a77fd54..8748e17 100644 --- a/test/results/defaultLang.inline=js.html +++ b/test/results/defaultLang.inline=js.html @@ -53,14 +53,8 @@

Default language

{ inline: 'js' }

-
-
const x = true;
-
+
const x = true;
+

+ 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); + } + +

test
+