Skip to content

Commit 5c3b277

Browse files
committed
Fix multiple codes in a pre
Closes GH-34.
1 parent 093fed2 commit 5c3b277

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,14 @@ export default function rehypeHighlight(options) {
101101
node.properties.className.unshift(name)
102102
}
103103

104+
const text = toText(node, {whitespace: 'pre'})
104105
/** @type {Root} */
105106
let result
106107

107108
try {
108109
result = lang
109-
? lowlight.highlight(lang, toText(parent), {prefix})
110-
: lowlight.highlightAuto(toText(parent), {prefix, subset})
110+
? lowlight.highlight(lang, text, {prefix})
111+
: lowlight.highlightAuto(text, {prefix, subset})
111112
} catch (error) {
112113
const cause = /** @type {Error} */ (error)
113114

test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,20 @@ test('rehypeHighlight', async function (t) {
366366
)
367367
})
368368

369+
await t.test('should support multiple `code`s in a `pre`', async function () {
370+
const file = await rehype()
371+
.data('settings', {fragment: true})
372+
.use(rehypeHighlight).process(`<pre>
373+
<code class="language-javascript">const a = 1;</code>
374+
<code class="language-python">printf("x")</code>
375+
</pre>`)
376+
377+
assert.equal(
378+
String(file),
379+
'<pre> <code class="hljs language-javascript"><span class="hljs-keyword">const</span> a = <span class="hljs-number">1</span>;</code>\n <code class="hljs language-python">printf(<span class="hljs-string">"x"</span>)</code>\n</pre>'
380+
)
381+
})
382+
369383
await t.test('should reprocess exact', async function () {
370384
const file = await rehype()
371385
.data('settings', {fragment: true})

0 commit comments

Comments
 (0)