1
0
mirror of https://github.com/jdan/98.css.git synced 2025-09-26 05:19:17 +02:00

split build:docs into its own command

This commit is contained in:
Jordan Scales
2020-04-24 19:21:25 -04:00
parent 1adb014673
commit c4b617ba4e
3 changed files with 61 additions and 64 deletions

45
build-docs.js Normal file
View File

@@ -0,0 +1,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 })
);