-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
259 lines (227 loc) · 7.04 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import autolinkHeadings from "rehype-autolink-headings";
import directive from "remark-directive";
import fs from "fs/promises";
import gfm from "remark-gfm";
import glob from "glob";
import h from "hastscript";
import highlight from "rehype-highlight";
import html from "rehype-stringify";
import markdown from "remark-parse";
import mkdirp from "mkdirp";
import path from "path";
import raw from "rehype-raw";
import remark2rehype from "remark-rehype";
import slug from "rehype-slug";
import toc from "remark-toc";
import unified from "unified";
import remark from "remark";
import { visit } from "unist-util-visit";
import escapeHtml from "@youtwitface/escape-html";
import find from "unist-util-find";
import { minify } from "html-minifier";
const root = ``;
const head = (title, description) => `
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;700&family=Source+Code+Pro&display=swap" rel="stylesheet">
<link rel="stylesheet" href="${root}/static/style.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css">
<script src="${root}/static/script.js" defer></script>
<title>${escapeHtml(title)} | GramJS</title>
<meta name="title" content="${escapeHtml(title)} | GramJS" />
<meta name="description" content="${escapeHtml(description)}" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://gram.js.org" />
<meta property="og:title" content="${escapeHtml(title)} | GramJS" />
<meta property="og:description" content="${escapeHtml(description)}" />
</head>
<body>`;
const foot = `
<script src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js"></script>
<script>
docsearch({
apiKey: '007607fb54223b523130d51c9c9d30b5',
indexName: 'gram',
inputSelector: '#docsearch',
});
</script>
</body>
</html>`;
const onDirective = (node) => {
const data = node.data || (node.data = {});
const hast = h(node.name, node.attributes, node.children);
if (hast.tagName === "tabs") {
data.hName = "div";
data.hProperties = {
class: `tabs`,
};
} else if (hast.tagName === "tab") {
data.hName = "div";
data.hProperties = {
class: `tab`,
"data-title": hast.properties.title,
};
}
};
const transform = (tree) =>
visit(
tree,
["textDirective", "leafDirective", "containerDirective"],
onDirective
);
const processor = unified()
.use(markdown)
.use(() => (tree) => {
const node = find(tree, { type: "heading" });
node.depth = Math.min(node.depth + 1, 6);
return tree;
})
.use(directive)
.use(() => transform)
.use(gfm)
.use(toc)
.use(remark2rehype, { allowDangerousHtml: true })
.use(slug)
.use(autolinkHeadings, { behavior: "append" })
.use(highlight)
.use(raw)
.use(html);
const treeProcessor = remark().use(() => (tree) => {
const headingIndex = tree.children.findIndex(
(child) => child.type === "heading" && child.depth === 1
);
const description = tree.children[headingIndex + 1];
description.type = "paragraph";
description.children = description.children.flatMap((child) =>
child.type === "link" ? child.children : child
);
return {
type: "root",
children: [
{
...tree.children[headingIndex],
type: "paragraph",
},
description,
],
};
});
const titleCase = (string) => {
if (string === "tl") {
return "TL";
}
return string
.split(/-/)
.map((x) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
.join(" ");
};
const linkTitle = (title, filePath, shouldLink = true) => {
const escapedTitle = escapeHtml(title);
return shouldLink
? `<a href="${root || ""}/${filePath}">${escapedTitle}</a>`
: escapedTitle;
};
const generateSidebar = (object, directories) => {
return Object.entries(object).reduce((sidebar, [key, value]) => {
const filePath = (directories ?? [])
.concat(key === "index" ? "" : key)
.join("/");
let hasSubList, title, children;
if (directories && key === "index") {
return sidebar;
} else if (typeof value === "string") {
title = linkTitle(value, filePath);
} else {
hasSubList = true;
title = linkTitle(titleCase(value.index || key), filePath, !!value.index);
children = `<ul data-sublist="${escapeHtml(filePath)}">${generateSidebar(
value,
(directories ?? []).concat(key)
)}</ul>`;
}
return (
sidebar +
`<li ${
hasSubList ? `data-li="${escapeHtml(filePath)}"` : ""
}>${title}</li>${children ?? ""}`
);
}, "");
};
glob("src/**/*.md", async (error, files) => {
if (error) {
console.error(error);
process.exit(1);
}
const fileData = await Promise.all(
files.map(async (file) => {
const data = await fs.readFile(file, { encoding: "utf-8" });
const doc = await processor.process(data);
const titleDoc = await treeProcessor.process(data);
let [title, ...description] = titleDoc.toString().split("\n");
title = title.trim();
description = description.join("\n").trim();
const { dir, name } = path.parse(file);
const directories = dir.split("/");
directories[0] = "docs, ...directive";
return {
doc,
name,
title,
description,
directories: directories.slice(1),
};
})
);
const tree = await fileData.reduce(
async (promise, { title, name, directories }) => {
const tree = await promise;
const currentTree = directories.reduce((t, d) => {
if (!(d in t)) {
t[d] = {};
}
return t[d];
}, tree);
currentTree[name] = directories[0] === "tl" ? name : title;
return tree;
},
Promise.resolve({})
);
const sidebar = generateSidebar(tree);
await Promise.all(
fileData.map(async ({ doc, title, description, name, directories }) => {
const fullPath = path.join("docs", ...directories);
await mkdirp(fullPath);
const html = `
${head(title, description.slice(0, 150))}
<header class="container">
<span class="menu-icon">
<svg viewBox="0 0 32 32" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="6.25%">
<path d="M4 8 L28 8 M4 16 L28 16 M4 24 L28 24"></path>
</svg>
</span>
<h1>GramJS</h1>
</header>
<main class="container page">
<div class="sidebar">
<ul>
<input id="docsearch" placeholder="Search...">
${sidebar}
</ul>
</div>
<div class="container content">${doc}</div>
</main>
${foot}
`;
await fs.writeFile(
`${fullPath}/${name}.html`,
minify(html, {
collapseWhitespace: true,
})
);
})
);
});