From bd2750d7dcc76b37e004ac42d4129183c1b31e08 Mon Sep 17 00:00:00 2001 From: Antoine Lambert Date: Fri, 23 Aug 2024 15:36:22 +0200 Subject: [PATCH 1/2] 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. --- lib/index.js | 7 ++++++- package.json | 3 ++- test/index.js | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index f965549..1412c8c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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)) { diff --git a/package.json b/package.json index d8e8dac..98d34d9 100644 --- a/package.json +++ b/package.json @@ -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 (https://github.com/Phryxia)" + "Sekyu Kwon (https://github.com/Phryxia)", + "Antoine Lambert (https://github.com/anlambert)" ] } \ No newline at end of file diff --git a/test/index.js b/test/index.js index 912900b..bfd6d54 100644 --- a/test/index.js +++ b/test/index.js @@ -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 @@ -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 From bc7e24ef9edfffad628bec13a3ff57eb337f4d69 Mon Sep 17 00:00:00 2001 From: Antoine Lambert Date: Fri, 23 Aug 2024 15:48:28 +0200 Subject: [PATCH 2/2] Add GitHub Actions job to execute tests --- .../workflows/showdown-highlight-tests.yml | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .github/workflows/showdown-highlight-tests.yml diff --git a/.github/workflows/showdown-highlight-tests.yml b/.github/workflows/showdown-highlight-tests.yml new file mode 100644 index 0000000..625f1f6 --- /dev/null +++ b/.github/workflows/showdown-highlight-tests.yml @@ -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