1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | const prismjs_1 = __importDefault(require("prismjs"));
|
6 | const strip_indent_1 = __importDefault(require("strip-indent"));
|
7 | const components_1 = __importDefault(require("prismjs/components/"));
|
8 |
|
9 | const components_2 = __importDefault(require("prismjs/components"));
|
10 | const prismAlias = Object.entries(components_2.default.languages).reduce((acc, [key, value]) => {
|
11 | if (value.alias) {
|
12 | if (Array.isArray(value.alias)) {
|
13 | value.alias.forEach(alias => (acc[alias] = key));
|
14 | }
|
15 | else if (typeof value.alias === 'string') {
|
16 | acc[value.alias] = key;
|
17 | }
|
18 | }
|
19 | return acc;
|
20 | }, {});
|
21 | const prismSupportedLanguages = Object.keys(components_2.default.languages).concat(Object.keys(prismAlias));
|
22 | const escape_html_1 = __importDefault(require("./escape_html"));
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function prismHighlight(code, language) {
|
29 |
|
30 | if (!prismjs_1.default.languages[language] && prismSupportedLanguages.includes(language))
|
31 | (0, components_1.default)(language);
|
32 | if (prismjs_1.default.languages[language]) {
|
33 |
|
34 | return prismjs_1.default.highlight(code, prismjs_1.default.languages[language], language);
|
35 | }
|
36 |
|
37 | return (0, escape_html_1.default)(code);
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function lineNumberUtil(code) {
|
44 | const matched = code.match(/\n(?!$)/g);
|
45 | const num = matched ? matched.length + 1 : 1;
|
46 | const lines = new Array(num + 1).join('<span></span>');
|
47 | return `<span aria-hidden="true" class="line-numbers-rows">${lines}</span>`;
|
48 | }
|
49 | function replaceTabs(str, tab) {
|
50 | return str.replace(/^\t+/gm, match => tab.repeat(match.length));
|
51 | }
|
52 | function PrismUtil(str, options = {}) {
|
53 | if (typeof str !== 'string')
|
54 | throw new TypeError('str must be a string!');
|
55 | str = (0, strip_indent_1.default)(str);
|
56 | const { lineNumber = true, lang = 'none', tab, mark, firstLine, isPreprocess = true, caption } = options;
|
57 |
|
58 | let language = lang === 'plaintext' || lang === 'none' ? 'none' : lang;
|
59 | if (prismAlias[language])
|
60 | language = prismAlias[language];
|
61 | const preTagClassArr = [];
|
62 | const preTagAttrArr = [];
|
63 | let preTagAttr = '';
|
64 | if (lineNumber)
|
65 | preTagClassArr.push('line-numbers');
|
66 | preTagClassArr.push(`language-${language}`);
|
67 |
|
68 |
|
69 | if (language !== 'none')
|
70 | preTagAttrArr.push(`data-language="${language}"`);
|
71 | if (!isPreprocess) {
|
72 |
|
73 |
|
74 | if (lineNumber && firstLine)
|
75 | preTagAttrArr.push(`data-start="${firstLine}"`);
|
76 |
|
77 |
|
78 | if (mark)
|
79 | preTagAttrArr.push(`data-line="${mark}"`);
|
80 |
|
81 |
|
82 | if (firstLine && mark)
|
83 | preTagAttrArr.push(`data-line-offset="${firstLine - 1}"`);
|
84 | }
|
85 | if (preTagAttrArr.length)
|
86 | preTagAttr = ' ' + preTagAttrArr.join(' ');
|
87 | if (tab)
|
88 | str = replaceTabs(str, tab);
|
89 | const codeCaption = caption ? `<div class="caption">${caption}</div>` : '';
|
90 | const startTag = `<pre class="${preTagClassArr.join(' ')}"${preTagAttr}>${codeCaption}<code class="language-${language}">`;
|
91 | const endTag = '</code></pre>';
|
92 | let parsedCode = '';
|
93 | if (language === 'none' || !isPreprocess) {
|
94 | parsedCode = (0, escape_html_1.default)(str);
|
95 | }
|
96 | else {
|
97 | parsedCode = prismHighlight(str, language);
|
98 | }
|
99 |
|
100 | if (lineNumber && isPreprocess) {
|
101 | parsedCode += lineNumberUtil(parsedCode);
|
102 | }
|
103 | return startTag + parsedCode + endTag;
|
104 | }
|
105 | module.exports = PrismUtil;
|
106 |
|
\ | No newline at end of file |