forked from botoxparty/XP.css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-docs.js
45 lines (40 loc) · 1.07 KB
/
build-docs.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
#!/usr/bin/env node
const dedent = require("dedent");
const ejs = require("ejs");
const fs = require("fs");
const glob = require("glob");
const hljs = require("highlight.js");
const path = require("path");
let id = 0;
function getNewId() {
return ++id;
}
function getCurrentId() {
return id;
}
const template = fs.readFileSync("docs/index.html.ejs", "utf-8");
function example(code) {
const magicBrackets = /\[\[(.*)\]\]/g;
const dedented = dedent(code);
const inline = dedented.replace(magicBrackets, "$1");
const escaped = hljs.highlight("html", dedented.replace(magicBrackets, ""))
.value;
return `<div class="example">
${inline}
<details>
<summary>Show code</summary>
<pre><code>${escaped}</code></pre>
</details>
</div>`;
}
glob("dist/*", (err, files) => {
if (!err) {
files.forEach((srcFile) =>
fs.copyFileSync(srcFile, path.join("docs", path.basename(srcFile)))
);
} else throw "error globbing dist directory.";
});
fs.writeFileSync(
"docs/index.html",
ejs.render(template, { getNewId, getCurrentId, example })
);