-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.ts
76 lines (67 loc) · 2.15 KB
/
site.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
import { ensureDir, frontMatter, marked, path } from "./deps/main.ts";
import type { Config, Post, PostAttributes } from "./types.ts";
export type { Config, Logger } from "./types.ts";
import { collectPostFiles, replaceExtname } from "./fs.ts";
export class Site {
constructor(
readonly config: Config,
) {}
static build(config: Config): Promise<void> {
const site = new Site(config);
return site.build();
}
async build(): Promise<void> {
const postFiles = await this.collectPostFiles();
const posts = await Promise.all(
postFiles.map((file) => this.buildPost(file)),
);
for (const post of posts) {
await this.writePost(post);
this.config.logger.info(`[info] Generated ${post.path}`);
}
}
private async collectPostFiles(): Promise<string[]> {
if (this.config.postFiles && this.config.postFiles.length > 0) {
return this.config.postFiles;
}
const postFiles = await collectPostFiles(this.config);
return postFiles;
}
private async buildPost(postFile: string): Promise<Post> {
const src = await Deno.readTextFile(postFile);
const stat = await Deno.lstat(postFile);
const { attributes, body } = frontMatter(src);
const defaultTitle = path.basename(postFile);
const post: Post = {
body: marked(body),
attributes: {
title: defaultTitle,
type: "article",
description: defaultTitle,
image: "",
...(attributes as Partial<PostAttributes>),
},
createdAt: stat.birthtime ?? undefined,
path: replaceExtname(
path.relative(this.config.postsDir, postFile),
".html",
),
};
const ctx = { config: this.config, post };
for (const plugin of this.config.plugins) {
await plugin.didBuildPost(ctx);
}
return post;
}
private async writePost(post: Post): Promise<void> {
await this.write(post.path, post.body);
}
private async write(
filename: string,
contents: string,
): Promise<void> {
const distFilename = path.join(this.config.distDir, filename);
await ensureDir(path.dirname(distFilename));
await Deno.writeTextFile(distFilename, contents);
}
}