-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
172 lines (155 loc) · 5.13 KB
/
build.ts
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
import fs from "node:fs/promises";
import path from "node:path";
import { load } from "js-yaml";
import _redirects from "./src/_redirects.js";
import NotFound from "./src/404.js";
import { wrapPage } from "./lib/page.js";
import { getCssText } from "./src/_includes/style.js";
import { Profile } from "./src/_data/profile.js";
import Works from "./src/works.js";
import SlideIndex from "./src/slides/index.js";
import getSlides from "./src/_data/slides.js";
import SlidePage from "./src/slides/SlidePage.js";
import SlideEmbed from "./src/slides/SlideEmbed.js";
import { getTalks } from "./src/talks/talks.js";
import TalkPage from "./src/_includes/layouts/TalkPage.js";
import TalkIndex from "./src/TalkIndex.js";
import { getPosts } from "./src/blog/post/posts.js";
import PostIndex from "./src/blog/PostIndex.js";
import BlogPost from "./src/_includes/layouts/BlogPost.js";
import rss from "./src/rss.js";
import { compile } from "./lib/bundle.js";
import Index from "./src/index.js";
const start = new Date();
console.log("honai.me generator started.");
const cwd = process.cwd();
const distDir = path.join(cwd, "public");
const srcDir = path.join(cwd, "src");
const staticDir = path.join(cwd, "static");
const SITE_DOMAIN = "www.honai.me";
async function buildClientJs() {
const clienJsFiles = (
await fs.readdir(path.join(srcDir, "js"), {
withFileTypes: true,
})
)
.filter((d) => d.isFile() && path.extname(d.name) === ".js")
.map((d) => ({
src: path.join(srcDir, "js", d.name),
dest: `/js/${d.name}` as AbsolutePath,
}));
await Promise.all(
clienJsFiles.map(async ({ src, dest }) => write(dest, await compile(src)))
);
}
async function build() {
const profile = load(
await fs.readFile(path.join(srcDir, "_data/profile.yaml"), {
encoding: "utf-8",
})
) as Profile;
const slides = await getSlides();
const talks = await getTalks();
const posts = await getPosts();
const paginatedPosts = paginate(posts, 15, `/blog/`);
const prevNextPosts = posts.map((p, i) => ({
post: p,
newerPost: i === 0 ? undefined : posts[i - 1],
olderPost: i === posts.length - 1 ? undefined : posts[i + 1],
}));
await fs.cp(staticDir, distDir, { recursive: true });
await Promise.all<Promise<void>[]>([
write("/_redirects", _redirects),
write("/404.html", wrapPage("/404.html", NotFound)),
writePage("/works/", (u) => wrapPage(u, () => Works({ profile }))),
// slides
writePage("/slides/", (u) => wrapPage(u, () => SlideIndex({ slides }))),
...slides.map((s) =>
writePage(`/slides/${s.slug}/`, (u) =>
wrapPage(u, () => SlidePage({ slide: s }))
)
),
...slides.map((s) =>
writePage(`/slides/embed/${s.slug}/`, (u) =>
wrapPage(u, () => SlideEmbed({ slide: s }))
)
),
// talks
writePage("/talks/", (u) => wrapPage(u, () => TalkIndex({ talks }))),
...talks.map((talk) =>
writePage(`/talks/${talk.slug}/`, (u) =>
wrapPage(u, () => TalkPage(talk))
)
),
// posts
...paginatedPosts.map((page) =>
writePage(page.currentHref, (u) =>
wrapPage(u, () => PostIndex({ ...page, posts: page.grouped }))
)
),
...prevNextPosts.map((props) =>
writePage(`/blog/post/${props.post.slug}/`, (u) =>
wrapPage(u, () => BlogPost(props))
)
),
// feed
write("/blog/rss.xml", rss(posts, SITE_DOMAIN)),
// index
writePage("/", (u) => wrapPage(u, () => Index({ profile, posts, talks }))),
]);
// css
await write("/styles/index.css", getCssText());
await fs.copyFile(
path.join(
cwd,
"node_modules/highlight.js/styles/tomorrow-night-bright.css"
),
path.join(distDir, "styles/highlight.css")
);
// JS
await buildClientJs();
}
type AbsolutePath = `/${string}`;
type TrailingSlash = `${AbsolutePath}/`;
async function write(absPath: AbsolutePath, content: string) {
const file = path.join(distDir, absPath);
const dir = path.dirname(file);
console.log(`Writing ${path.relative(cwd, file)}`);
await fs.mkdir(dir, { recursive: true });
await fs.writeFile(file, content, { encoding: "utf-8" });
}
async function writePage(
canonicalPath: TrailingSlash | `/`,
Fn: (url: string) => string
) {
const file = path.join(canonicalPath, "index.html") as AbsolutePath;
await write(file, Fn(canonicalPath));
}
type Page<T> = {
prevHref?: TrailingSlash;
currentHref: TrailingSlash;
nextHref?: TrailingSlash;
total: number;
currentIdx: number;
grouped: T[];
};
function paginate<T>(data: T[], by: number, baseUrl: TrailingSlash): Page<T>[] {
// 0番目はbaseurlそのまま
const toHref = (i: number): TrailingSlash =>
i === 0 ? baseUrl : `${baseUrl}${i.toString(10)}/`;
const groups = Math.ceil(data.length / by);
return Array(groups)
.fill(0)
.map((_, i) => ({
currentIdx: i,
total: groups,
grouped: data.slice(i * by, (i + 1) * by),
prevHref: i === 0 ? undefined : toHref(i - 1),
currentHref: toHref(i),
nextHref: i === groups - 1 ? undefined : toHref(i + 1),
}));
}
build().then(() => {
const time = new Date().valueOf() - start.valueOf();
console.log(`Finished in ${time / 1000}s.`);
});