|
1 | 1 | "use strict";
|
2 | 2 |
|
3 | 3 | 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 | + ; |
8 | 8 |
|
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]; |
20 | 20 |
|
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 | + } |
35 | 31 |
|
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; |
37 | 36 | }
|
38 |
| - } |
39 |
| - ]; |
| 37 | + } |
| 38 | + ; |
| 39 | + |
| 40 | + return showdown.helper.replaceRecursiveRegExp(text, replacement, left, right, flags); |
| 41 | + } |
| 42 | + } |
| 43 | + ]; |
40 | 44 | };
|
0 commit comments