Skip to content

Commit c20cac4

Browse files
committed
Merge branch 'pre-class' of https://github.com/devbaraus/showdown-highlight into new-version
2 parents 0f04534 + 8f3d923 commit c20cac4

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

lib/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { ShowdownExtension } from 'showdown';
1+
import type { ShowdownExtension } from "showdown";
22

3-
declare function showdownHighlight(): ShowdownExtension[];
3+
declare function showdownHighlight({
4+
pre: Boolean = false,
5+
}): ShowdownExtension[];
46
export = showdownHighlight;

lib/index.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
"use strict";
22

33
const decodeHtml = require("html-encoder-decoder").decode
4-
, showdown = require("showdown")
5-
, hljs = require("highlight.js")
6-
, classAttr = 'class="'
7-
;
4+
, showdown = require("showdown")
5+
, hljs = require("highlight.js")
6+
, classAttr = 'class="'
7+
;
88

9-
module.exports = function showdownHighlight () {
10-
return [
11-
{
12-
type: "output"
13-
, filter (text, converter, options) {
14-
let left = "<pre><code\\b[^>]*>"
15-
, right = "</code></pre>"
16-
, flags = "g"
17-
, replacement = (wholeMatch, match, left, right) => {
18-
match = decodeHtml(match);
19-
let lang = (left.match(/class=\"([^ \"]+)/) || [])[1];
9+
module.exports = function showdownHighlight({ pre = false }) {
10+
return [
11+
{
12+
type: "output"
13+
, filter(text, converter, options) {
14+
let left = "<pre><code\\b[^>]*>"
15+
, right = "</code></pre>"
16+
, flags = "g"
17+
, replacement = (wholeMatch, match, left, right) => {
18+
match = decodeHtml(match);
19+
let lang = (left.match(/class=\"([^ \"]+)/) || [])[1];
2020

21-
if (left.includes(classAttr)) {
22-
let attrIndex = left.indexOf(classAttr) + classAttr.length;
23-
left = left.slice(0, attrIndex) + 'hljs ' + left.slice(attrIndex);
24-
} else {
25-
left = left.slice(0, -1) + ' class="hljs">';
26-
}
27-
28-
if (lang && hljs.getLanguage(lang)) {
29-
return left + hljs.highlight(lang, match).value + right;
30-
} else {
31-
return left + hljs.highlightAuto(match).value + right;
32-
}
33-
}
34-
;
21+
if (left.includes(classAttr)) {
22+
let attrIndex = left.indexOf(classAttr) + classAttr.length;
23+
left = left.slice(0, attrIndex) + 'hljs ' + left.slice(attrIndex);
24+
} else {
25+
left = left.slice(0, -1) + ' class="hljs">';
26+
}
27+
28+
if (pre && lang) {
29+
left = left.replace('<pre>', `<pre class="${lang} language-${lang}">`);
30+
}
3531

36-
return showdown.helper.replaceRecursiveRegExp(text, replacement, left, right, flags);
32+
if (lang && hljs.getLanguage(lang)) {
33+
return left + hljs.highlight(lang, match).value + right;
34+
} else {
35+
return left + hljs.highlightAuto(match).value + right;
3736
}
38-
}
39-
];
37+
}
38+
;
39+
40+
return showdown.helper.replaceRecursiveRegExp(text, replacement, left, right, flags);
41+
}
42+
}
43+
];
4044
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"contributors": [
5353
"Cristiano Ribeiro <expedit@gmail.com> (https://github.com/expedit85)",
5454
"obedm503 (https://obedm503.github.io)",
55-
"Ariel Shaqed (Scolnicov) (https://github.com/arielshaqed)"
55+
"Ariel Shaqed (Scolnicov) (https://github.com/arielshaqed)",
56+
"Bruno de Araújo Alves (devbaraus) (https://github.com/devbaraus)"
5657
]
5758
}

0 commit comments

Comments
 (0)