Skip to content

Commit

Permalink
Ensure to strip any code block annotation after language name
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
anlambert committed Aug 23, 2024
1 parent 1984968 commit 2c92530
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ module.exports = function showdownHighlight({ pre = false, auto_detection = true
const replacement = (wholeMatch, match, left, right) => {
match = decodeHtml(match)

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

if (!lang && !auto_detection) {
return wholeMatch
} else if (lang && lang.indexOf(',') > 0) {
// ensure to strip any code block annotation line no_run for instance
const langNoAnnotation = lang.slice(0, lang.indexOf(','));
left = left.replace(new RegExp(lang, 'g'), langNoAnnotation);
lang = langNoAnnotation;
}

if (left.includes(classAttr)) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"obedm503 (https://obedm503.github.io)",
"Ariel Shaqed (Scolnicov) (https://github.com/arielshaqed)",
"Bruno de Araújo Alves (devbaraus) (https://github.com/devbaraus)",
"Sekyu Kwon <[email protected]> (https://github.com/Phryxia)"
"Sekyu Kwon <[email protected]> (https://github.com/Phryxia)",
"Antoine Lambert <[email protected]> (https://github.com/anlambert)"
]
}
15 changes: 14 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ function sayHello (msg, who) {
return \`\${who} says: msg\`;
}
sayHello("Hello World", "Johnny");
\`\`\``

const CODEBLOCK_WITHOUT_LANGUAGE = `
\`\`\`
function sayHello (msg, who) {
return \`\${who} says: msg\`;
}
sayHello("Hello World", "Johnny");
\`\`\``

const CODEBLOCK_WITH_LANGUAGE_AND_ANNOTATION = `
\`\`\`rust,no_run
fn do_amazing_thing() -> i32 {
unimplemented!()
}
\`\`\``

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

t.should("work with code block language and annotation", () => {
let html = converter.makeHtml(CODEBLOCK_WITH_LANGUAGE_AND_ANNOTATION);
t.expect(html.includes('class="hljs rust language-rust"')).toEqual(true);
t.expect(html.includes("hljs-type")).toEqual(true);
});

const converter_auto_disabled = new showdown.Converter({
extensions: [showdownHighlight({
auto_detection: false
Expand Down

0 comments on commit 2c92530

Please sign in to comment.