-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
highlight.js
132 lines (102 loc) · 3.83 KB
/
highlight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';
const hljs = require('highlight.js');
const stripIndent = require('strip-indent');
const alias = require('../highlight_alias.json');
function highlightUtil(str, options = {}) {
if (typeof str !== 'string') throw new TypeError('str must be a string!');
str = stripIndent(str);
const useHljs = Object.prototype.hasOwnProperty.call(options, 'hljs') ? options.hljs : false;
const {
gutter = true,
firstLine = 1,
caption,
mark = [],
languageAttr = false,
tab
} = options;
let { wrap = true } = options;
hljs.configure({ classPrefix: useHljs ? 'hljs-' : ''});
const data = highlight(str, options);
const lang = options.lang || data.language || '';
const classNames = (useHljs ? 'hljs' : 'highlight') + (lang ? ` ${lang}` : '');
if (gutter && !wrap) wrap = true; // arbitrate conflict ("gutter:true" takes priority over "wrap:false")
const before = useHljs ? `<pre><code class="${classNames}"${languageAttr && lang ? ` data-language="${lang}"` : ''}>` : '<pre>';
const after = useHljs ? '</code></pre>' : '</pre>';
const lines = data.value.split('\n');
let numbers = '';
let content = '';
for (let i = 0, len = lines.length; i < len; i++) {
let line = lines[i];
if (tab) line = replaceTabs(line, tab);
numbers += `<span class="line">${Number(firstLine) + i}</span><br>`;
content += formatLine(line, Number(firstLine) + i, mark, options, wrap);
}
let codeCaption = '';
if (caption) {
codeCaption = wrap ? `<figcaption>${caption}</figcaption>` : `<div class="caption">${caption}</div>`;
}
if (!wrap) {
// if original content has one trailing newline, replace it only once, else remove all trailing newlines
content = /\r?\n$/.test(data.value) ? content.replace(/\n$/, '') : content.trimEnd();
return `<pre>${codeCaption}<code class="${classNames}"${languageAttr && lang ? ` data-language="${lang}"` : ''}>${content}</code></pre>`;
}
let result = `<figure class="highlight${data.language ? ` ${data.language}` : ''}"${languageAttr && lang ? ` data-language="${lang}"` : ''}>`;
result += codeCaption;
result += '<table><tr>';
if (gutter) {
result += `<td class="gutter"><pre>${numbers}</pre></td>`;
}
result += `<td class="code">${before}${content}${after}</td>`;
result += '</tr></table></figure>';
return result;
}
function formatLine(line, lineno, marked, options, wrap) {
const useHljs = (options.hljs || false) || !wrap;
const br = wrap ? '<br>' : '\n';
let res = useHljs ? '' : '<span class="line';
if (marked.includes(lineno)) {
// Handle marked lines.
res += useHljs ? `<mark>${line}</mark>` : ` marked">${line}</span>`;
} else {
res += useHljs ? line : `">${line}</span>`;
}
res += br;
return res;
}
function replaceTabs(str, tab) {
return str.replace(/\t+/, match => tab.repeat(match.length));
}
function highlight(str, options) {
let { lang } = options;
const { autoDetect = false } = options;
if (lang) {
lang = lang.toLowerCase();
} else if (autoDetect) {
const result = hljs.highlightAuto(str);
return closeTags(result);
}
if (!lang || !alias.aliases[lang]) {
lang = 'plaintext';
}
const res = hljs.highlight(str, {
language: lang,
ignoreIllegals: true
});
return closeTags(res);
}
// https://github.com/hexojs/hexo-util/issues/10
function closeTags(res) {
const tokenStack = [];
res.value = res.value.split('\n').map(line => {
const prepend = tokenStack.map(token => `<span class="${token}">`).join('');
const matches = line.matchAll(/(<span class="(.*?)">|<\/span>)/g);
for (const match of matches) {
if (match[0] === '</span>') tokenStack.shift();
else tokenStack.unshift(match[2]);
}
const append = '</span>'.repeat(tokenStack.length);
return prepend + line + append;
}).join('\n');
return res;
}
module.exports = highlightUtil;