Skip to content

Commit 6d71163

Browse files
committed
Add test
1 parent 0e11f47 commit 6d71163

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

index.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,20 @@ function attacher(options) {
6767
? lowlight.highlight(lang, toText(parent), options)
6868
: lowlight.highlightAuto(toText(parent), options)
6969
} catch (error) {
70-
if (
71-
error &&
72-
settings.ignoreMissing &&
73-
/Unknown language/.test(error.message)
74-
) {
75-
return
70+
if (!settings.ignoreMissing || !/Unknown language/.test(error.message)) {
71+
throw error
7672
}
7773

78-
throw error
74+
result = {}
7975
}
80-
81-
if (!lang) {
82-
if (result.language) {
83-
props.className.push('language-' + result.language)
84-
} else {
85-
// result.value is empty when highlightjs cant autodetect language
86-
result.value = node.children
87-
}
76+
77+
if (!lang && result.language) {
78+
props.className.push('language-' + result.language)
8879
}
8980

90-
node.children = result.value
81+
if (result.value) {
82+
node.children = result.value
83+
}
9184
}
9285
}
9386

test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ test('highlight()', function (t) {
290290
'should not highlight plainText-ed languages'
291291
)
292292

293+
t.equal(
294+
rehype()
295+
.data('settings', {fragment: true})
296+
.use(highlight, {subset: ['cpp']})
297+
.processSync(`<pre><code>def add(a, b):\n return a + b</code></pre>`)
298+
.toString(),
299+
'<pre><code class="hljs language-cpp"><span class="hljs-function">def <span class="hljs-title">add</span><span class="hljs-params">(a, b)</span>:\n <span class="hljs-keyword">return</span> a + b</span></code></pre>',
300+
'should not remove contents'
301+
)
302+
293303
t.equal(
294304
rehype()
295305
.data('settings', {fragment: true})

0 commit comments

Comments
 (0)