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

Fix #41 - Add auto_detection option to control hljs' language detection feature #43

Merged
merged 5 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
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
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ShowdownExtension } from "showdown";

declare function showdownHighlight({ pre: Boolean }): ShowdownExtension[];
declare function showdownHighlight({ pre: Boolean, auto_detection: Boolean }): 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
}

Comment on lines +44 to +47
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Early return if auto_detection is false and there is no language information. This prevents redundant operations.

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 = `
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's repetitively used, I extracted as constant.

\`\`\`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({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since converter is not changed, I used const.

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