Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure to strip any code block annotation after language name #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/showdown-highlight-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: showdown-highlight-tests

on: [push, pull_request]

jobs:
tests:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
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 after language
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