-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (88 loc) · 2.69 KB
/
index.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
const pug = require("pug");
const path = require("path");
const index = path.resolve(__dirname, "./index.pug");
function catalogsTree(catalogs) {
let tree = [];
while (catalogs.length) {
const catalog = catalogs.shift();
if (catalog.level === 1) {
tree.push({
title: catalog.text,
path: `#${catalog.id}`,
level: catalog.level,
content: catalog.content,
text: catalog.text,
children: []
});
} else {
let end = tree[tree.length - 1];
if (!end) {
end = {
children: []
};
tree.push(end);
}
let current = end;
for (let index = 0; index < catalog.level - 2; index++) {
current.children = current.children || [];
let obj = current.children[current.children.length - 1];
if (!obj) {
obj = {
children: []
};
current.children.push(obj);
}
current = obj;
}
current.children.push({
title: catalog.text,
path: `#${catalog.id}`,
level: catalog.level,
content: catalog.content,
text: catalog.text,
children: []
});
}
}
return tree;
}
function getSideBar(ctx) {
const sideBars = {};
ctx.fsEach(function(file) {
file.extname = ".html";
const md = file.md;
const setting = md.setting;
if (setting) {
sideBars[setting.category] = sideBars[setting.category] || [];
sideBars[setting.category].push({
title: setting.title,
path: file.relative,
children: catalogsTree(md.catalogs)
});
}
});
return sideBars;
}
module.exports = async function(ctx) {
ctx.hook.add("dist.before", function(files) {
const hljsStyle = ctx.data.mdHljsStyle;
const hljscss = "hljs.css";
if (hljsStyle) {
ctx.fsWrite(path.join(ctx.data.output, hljscss), hljsStyle);
}
const sides = getSideBar(ctx);
ctx.fsEach(function(file) {
const contents = pug.renderFile(
index,
Object.assign(
{
sides: sides,
hljscss: hljsStyle ? hljscss : ""
},
file.md
)
);
file.contents = new Buffer(contents);
});
});
};