Skip to content

Commit bd2750d

Browse files
committed
Ensure to strip any code block annotation after language name
Some markdown files can contain extra code block annotations after the language name, separated by commas. Such extra annotations break code highlighting after processing by showdown-highlight as the language name ends up invalid and cannot be found by highlight.js. So ensure to strip these extra annotations to fix HTML rendering.
1 parent 1984968 commit bd2750d

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

lib/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ module.exports = function showdownHighlight({ pre = false, auto_detection = true
5151
const replacement = (wholeMatch, match, left, right) => {
5252
match = decodeHtml(match)
5353

54-
const lang = (left.match(/class=\"([^ \"]+)/) || [])[1]
54+
let lang = (left.match(/class=\"([^ \"]+)/) || [])[1]
5555

5656
if (!lang && !auto_detection) {
5757
return wholeMatch
58+
} else if (lang && lang.indexOf(',') > 0) {
59+
// ensure to strip any code block annotation after language
60+
const langNoAnnotation = lang.slice(0, lang.indexOf(','));
61+
left = left.replace(new RegExp(lang, 'g'), langNoAnnotation);
62+
lang = langNoAnnotation;
5863
}
5964

6065
if (left.includes(classAttr)) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"obedm503 (https://obedm503.github.io)",
5555
"Ariel Shaqed (Scolnicov) (https://github.com/arielshaqed)",
5656
"Bruno de Araújo Alves (devbaraus) (https://github.com/devbaraus)",
57-
"Sekyu Kwon <[email protected]> (https://github.com/Phryxia)"
57+
"Sekyu Kwon <[email protected]> (https://github.com/Phryxia)",
58+
"Antoine Lambert <[email protected]> (https://github.com/anlambert)"
5859
]
5960
}

test/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ function sayHello (msg, who) {
1212
return \`\${who} says: msg\`;
1313
}
1414
sayHello("Hello World", "Johnny");
15-
1615
\`\`\``
16+
1717
const CODEBLOCK_WITHOUT_LANGUAGE = `
1818
\`\`\`
1919
function sayHello (msg, who) {
2020
return \`\${who} says: msg\`;
2121
}
2222
sayHello("Hello World", "Johnny");
23+
\`\`\``
24+
25+
const CODEBLOCK_WITH_LANGUAGE_AND_ANNOTATION = `
26+
\`\`\`rust,no_run
27+
fn do_amazing_thing() -> i32 {
28+
unimplemented!()
29+
}
2330
\`\`\``
2431

2532
// After requiring the module, use it as extension
@@ -42,6 +49,12 @@ sayHello("Hello World", "Johnny");
4249
t.expect(html.includes('class="hljs"')).toEqual(true);
4350
});
4451

52+
t.should("work with code block language and annotation", () => {
53+
let html = converter.makeHtml(CODEBLOCK_WITH_LANGUAGE_AND_ANNOTATION);
54+
t.expect(html.includes('class="hljs rust language-rust"')).toEqual(true);
55+
t.expect(html.includes("hljs-type")).toEqual(true);
56+
});
57+
4558
const converter_auto_disabled = new showdown.Converter({
4659
extensions: [showdownHighlight({
4760
auto_detection: false

0 commit comments

Comments
 (0)