-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathhighlighter.js
51 lines (45 loc) · 1.3 KB
/
highlighter.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
import { getHighlighter } from "shiki";
const supportedLangs = [
"bash",
"ballerina",
"toml",
"yaml",
"sh",
"json",
"graphql",
"sql",
"java",
"xml",
"cmd"
];
String.prototype.hashCode = function () {
var hash = 0,
i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
return hash;
}
const highlight = async (content) => {
const highlighter = await getHighlighter({
theme: 'github-light',
langs: supportedLangs,
});
let codes = new Map();
const regex = /```(\w+)([\s\S]*?)```/g;
let match = [];
while (match = regex.exec(content)) {
let code = match[2];
const firstLine = code.split('/n')[0];
const indent = firstLine.length - firstLine.trimStart().length;
const key = code.trim().split(/\r?\n/).map(row => row.trim()).join('\n');
code = code.split(/\r?\n/).map(row => row.substring(indent - 1)).join('\n');
const lang = (match[1]).toLowerCase();
codes.set(key.hashCode(), highlighter.codeToHtml(code.trim(), { lang: supportedLangs.includes(lang) ? lang : '' }));
}
return JSON.stringify([...codes]);
}
export { highlight };