-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.tsx
executable file
·74 lines (63 loc) · 2.28 KB
/
build.tsx
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
#!/usr/bin/env -S deno run --config deno.jsonc --allow-read=. --allow-write=./dist --allow-net --no-check
/** @jsx h */
import {
h,
renderSSR,
} from "https://cdn.jsdelivr.net/gh/justjavac/deno_docx@latest/deps.ts";
import type { Toc } from "https://cdn.jsdelivr.net/gh/justjavac/deno_docx@latest/components/Sidebar.tsx";
import App from "https://cdn.jsdelivr.net/gh/justjavac/deno_docx@latest/App.tsx";
// clean `/dist` dir
await Deno.remove("dist", { recursive: true }).catch(() => {});
const toc: Toc = JSON.parse(await Deno.readTextFile("toc.json"));
for (const [path, { name, children }] of Object.entries(toc)) {
createHtml(path);
if (children == null) continue;
for (const [subPath, subName] of Object.entries(children)) {
createHtml(`${path}/${subPath}`);
}
}
// await copyImages();
await downloadCss();
await downloadFavicon();
async function createHtml(path: string) {
console.log("create file %s", path);
await Deno.mkdir(`dist/${path}`, { recursive: true }).catch(() => {});
const content = await Deno.readTextFile(`${path}.md`);
const savePath = `dist/${path}/index.html`;
const html = renderSSR(
<App
toc={toc}
content={content}
github="https://github.com/denocn/deno_docs"
/>,
);
await Deno.writeTextFile(savePath, html);
}
async function copyImages() {
console.log("copy images");
await Deno.mkdir("dist/images", { recursive: true });
for await (const dirEntry of Deno.readDir("images")) {
await Deno.copyFile(
`images/${dirEntry.name}`,
`dist/images/${dirEntry.name}`,
);
}
}
async function downloadCss() {
console.log("download css");
await Deno.mkdir(`dist/public`, { recursive: true }).catch(() => {});
const response = await fetch(
"https://cdn.jsdelivr.net/gh/justjavac/deno_docx@main/public/style.css",
);
const buffer = await response.arrayBuffer();
await Deno.writeFile("dist/public/style.css", new Uint8Array(buffer));
}
async function downloadFavicon() {
console.log("download favicon");
await Deno.mkdir(`dist/public`, { recursive: true }).catch(() => {});
const response = await fetch(
"https://cdn.jsdelivr.net/gh/justjavac/deno_docx@main/public/favicon.ico",
);
const buffer = await response.arrayBuffer();
await Deno.writeFile("dist/favicon.ico", new Uint8Array(buffer));
}