-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathhighlight.js
139 lines (107 loc) · 3.5 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
133
134
135
136
137
138
139
'use strict';
const hljs = require('highlight.js');
const stripIndent = require('strip-indent');
const alias = require('../highlight_alias.json');
const escapeHTML = require('./escape_html');
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 = [],
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}">` : '<pre>';
const after = useHljs ? '</code></pre>' : '</pre>';
if (!wrap) return `<pre><code class="${classNames}">${data.value}</code></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);
}
let result = `<figure class="highlight${data.language ? ` ${data.language}` : ''}">`;
if (caption) {
result += `<figcaption>${caption}</figcaption>`;
}
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) {
const useHljs = options.hljs || false;
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 => {
let result = '';
for (let i = 0, len = match.length; i < len; i++) {
result += tab;
}
return result;
});
}
function highlight(str, options) {
let { lang } = options;
const { autoDetect = false } = options;
if (!lang && autoDetect) {
const result = hljs.highlightAuto(str);
if (result.relevance > 0 && result.language) lang = result.language;
}
if (!lang) {
lang = 'plain';
}
const result = {
value: escapeHTML(str),
language: lang.toLowerCase()
};
if (result.language === 'plain') {
return result;
}
if (!alias.aliases[result.language]) {
result.language = 'plain';
return result;
}
return tryHighlight(str, result.language) || result;
}
function tryHighlight(str, lang) {
try {
const matching = str.match(/(\r?\n)/);
const separator = matching ? matching[1] : '';
const lines = matching ? str.split(separator) : [str];
let result = hljs.highlight(lang, lines.shift());
let html = result.value;
while (lines.length > 0) {
result = hljs.highlight(lang, lines.shift(), false, result.top);
html += separator + result.value;
}
result.value = html;
return result;
} catch (err) {
}
}
module.exports = highlightUtil;