diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 5fcf2d7..8bdf1a2 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -21,3 +21,10 @@ let converter = new showdown.Converter({ }) ``` +If you want to disable language [auto detection](https://highlightjs.org/usage/) feature of hljs, change `auto_detection` flag as `false`. With this option turned off, `showdown-highlight` will not process any codeblocks with no language specified. + +```js +let converter = new showdown.Converter({ + extensions: [showdownHighlight({ auto_detection: false })] +}) +``` \ No newline at end of file diff --git a/example/index.js b/example/index.js index 6a55625..0febfd3 100644 --- a/example/index.js +++ b/example/index.js @@ -8,8 +8,10 @@ const showdown = require('showdown') let converter = new showdown.Converter({ // That's it extensions: [showdownHighlight({ - // Whether to add the classes to the
tag + // Whether to add the classes to thetag, default is false pre: true + // Whether to use hljs' auto language detection, default is true + , auto_detection: true })] }); diff --git a/lib/index.d.ts b/lib/index.d.ts index b084074..9784036 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,4 +1,9 @@ import type { ShowdownExtension } from "showdown"; -declare function showdownHighlight({ pre: Boolean }): ShowdownExtension[]; +declare type ShowdownHighlightOptions = { + pre: boolean + auto_detection: boolean +} + +declare function showdownHighlight(options?: Partial): ShowdownExtension[]; export = showdownHighlight; diff --git a/lib/index.js b/lib/index.js index d0bfdd1..d8ec2eb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -28,7 +28,7 @@ const decodeHtml = require("html-encoder-decoder").decode * @name showdownHighlight * @function */ -module.exports = function showdownHighlight({ pre = false } = {}) { +module.exports = function showdownHighlight({ pre = false, auto_detection = true } = {}) { const filter = (text, converter, options) => { const params = { left: " ]*>" @@ -41,6 +41,10 @@ module.exports = function showdownHighlight({ pre = false } = {}) { const lang = (left.match(/class=\"([^ \"]+)/) || [])[1] + if (!lang && !auto_detection) { + return wholeMatch + } + if (left.includes(classAttr)) { const attrIndex = left.indexOf(classAttr) + classAttr.length left = left.slice(0, attrIndex) + 'hljs ' + left.slice(attrIndex) diff --git a/package.json b/package.json index 1681fa7..d8e8dac 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "Cristiano Ribeiro
(https://github.com/expedit85)", "obedm503 (https://obedm503.github.io)", "Ariel Shaqed (Scolnicov) (https://github.com/arielshaqed)", - "Bruno de Araújo Alves (devbaraus) (https://github.com/devbaraus)" + "Bruno de Araújo Alves (devbaraus) (https://github.com/devbaraus)", + "Sekyu Kwon (https://github.com/Phryxia)" ] } \ No newline at end of file diff --git a/test/index.js b/test/index.js index eee9186..912900b 100644 --- a/test/index.js +++ b/test/index.js @@ -6,21 +6,30 @@ const tester = require("tester") ; tester.describe("showdown-highlight", t => { - // After requiring the module, use it as extension - let converter = new showdown.Converter({ - extensions: [showdownHighlight] - }); - - t.should("A Showdown extension for highlight the code blocks.", () => { - // Now you can Highlight code blocks - let html = converter.makeHtml(` + const CODEBLOCK_WITH_LANGUAGE = ` \`\`\`js 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"); +\`\`\`` + + // After requiring the module, use it as extension + const converter = new showdown.Converter({ + extensions: [showdownHighlight] + }); + + t.should("A Showdown extension for highlight the code blocks.", () => { + // Now you can Highlight code blocks + let html = converter.makeHtml(CODEBLOCK_WITH_LANGUAGE); t.expect(html.includes('class="hljs js language-js"')).toEqual(true); t.expect(html.includes("hljs-string")).toEqual(true); @@ -28,15 +37,42 @@ sayHello("Hello World", "Johnny"); t.should("work without code block language", () => { // Now you can Highlight code blocks - let html = converter.makeHtml(` -\`\`\` -function sayHello (msg, who) { - return \`\${who} says: msg\`; -} -sayHello("Hello World", "Johnny"); -\`\`\` - `); + let html = converter.makeHtml(CODEBLOCK_WITHOUT_LANGUAGE); t.expect(html.includes('class="hljs"')).toEqual(true); }); + + const converter_auto_disabled = new showdown.Converter({ + extensions: [showdownHighlight({ + auto_detection: false + })] + }) + const converter_auto_disabled_with_pre = new showdown.Converter({ + extensions: [showdownHighlight({ + auto_detection: false + , pre: true + })] + }) + + t.should("process code block with language, when auto_detection disabled", () => { + t.expect(converter_auto_disabled + .makeHtml(CODEBLOCK_WITH_LANGUAGE) + .includes('class="hljs js language-js"')) + .toEqual(true); + t.expect(converter_auto_disabled_with_pre + .makeHtml(CODEBLOCK_WITH_LANGUAGE) + .includes('class="hljs js language-js"')) + .toEqual(true); + }) + + t.should("not process code block with no language, when auto_detection disabled", () => { + t.expect(converter_auto_disabled + .makeHtml(CODEBLOCK_WITHOUT_LANGUAGE) + .includes('hljs')) + .toEqual(false) + t.expect(converter_auto_disabled_with_pre + .makeHtml(CODEBLOCK_WITHOUT_LANGUAGE) + .includes('hljs')) + .toEqual(false) + }) });