-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpug.config.js
57 lines (48 loc) · 1.36 KB
/
pug.config.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
const { JSDOM } = require("jsdom");
const { languages, highlight } = require("prismjs");
require("prismjs/components/prism-json");
require("prismjs/components/prism-bash");
require("prismjs/components/prism-typescript");
const getLang = (className) => {
switch (className) {
case "language-json":
return "json";
case "language-javascript":
case "language-js":
return "javascript";
case "language-typescript":
return "typescript";
case "language-cmd":
return "shell";
case "language-html":
case "language-xml":
return "markup";
default:
return undefined;
}
};
module.exports = {
locals: {
ci: process.env.CI,
},
filters: {
highlight: function (text, options) {
const dom = new JSDOM(`<!DOCTYPE html><body>${text}</body>`);
const codeBlocks = [...dom.window.document.querySelectorAll("code")];
const isCodeBlocksFound = codeBlocks.length;
if (!isCodeBlocksFound) {
return text;
}
codeBlocks.forEach((node) => {
const code = node.textContent;
const maybeLanguage = node.className;
const language = getLang(maybeLanguage);
if (language) {
node.innerHTML = highlight(code, languages[language], language);
}
});
const result = dom.window.document.body.innerHTML;
return result;
},
},
};