Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Phryxia/showdown-highlight into n…
Browse files Browse the repository at this point in the history
…ew-version
  • Loading branch information
IonicaBizau committed Sep 18, 2022
2 parents 95f930e + 510a9bc commit 700cb4f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 21 deletions.
7 changes: 7 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 })]
})
```
4 changes: 3 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pre> tag
// Whether to add the classes to the <pre> tag, default is false
pre: true
// Whether to use hljs' auto language detection, default is true
, auto_detection: true
})]
});

Expand Down
7 changes: 6 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<ShowdownHighlightOptions>): ShowdownExtension[];
export = showdownHighlight;
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<pre><code\\b[^>]*>"
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"Cristiano Ribeiro <expedit@gmail.com> (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 <xahhaepica@gmail.com> (https://github.com/Phryxia)"
]
}
70 changes: 53 additions & 17 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,73 @@ 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);
});

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)
})
});

0 comments on commit 700cb4f

Please sign in to comment.