-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
highlight.js
160 lines (121 loc) · 3.79 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
const hljs = require('highlight.js/lib/highlight');
const Entities = require('html-entities').XmlEntities;
const entities = new Entities();
const alias = require('../highlight_alias.json');
function highlightUtil(str, options = {}) {
if (typeof str !== 'string') throw new TypeError('str must be a string!');
const useHljs = options.hasOwnProperty('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);
if (useHljs && !gutter) wrap = false;
const before = useHljs ? `<pre><code class="hljs ${options.lang}">` : '<pre>';
const after = useHljs ? '</code></pre>' : '</pre>';
if (!wrap) return useHljs ? before + data.value + after : data.value;
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">${firstLine + i}</span><br>`;
content += formatLine(line, 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 encodePlainString(str) {
return entities.encode(str);
}
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 loadLanguage(lang) {
hljs.registerLanguage(lang, require(`highlight.js/lib/languages/${lang}`));
}
function tryLanguage(lang) {
if (hljs.getLanguage(lang)) return true;
if (!alias.aliases[lang]) return false;
loadLanguage(alias.aliases[lang]);
return true;
}
function loadAllLanguages() {
alias.languages.filter(lang => !hljs.getLanguage(lang)).forEach(loadLanguage);
}
function highlight(str, options) {
let { lang } = options;
const { autoDetect = false } = options;
if (!lang && autoDetect) {
loadAllLanguages();
const result = hljs.highlightAuto(str);
if (result.relevance > 0 && result.language) lang = result.language;
}
if (!lang) {
lang = 'plain';
}
const result = {
value: encodePlainString(str),
language: lang.toLowerCase()
};
if (result.language === 'plain') {
return result;
}
if (!tryLanguage(result.language)) {
result.language = 'plain';
return result;
}
if (options.hljs) return hljs.highlight(lang, str);
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;