-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebuild.ts
112 lines (96 loc) · 3.4 KB
/
prebuild.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
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkFrontmatter from "remark-frontmatter";
import remarkExtractFrontmatter from "remark-extract-frontmatter";
import YAML from "yaml";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
import { readFile, writeFile } from "@foxkit/node-util/fs";
import { readFileYaml } from "@foxkit/node-util/fs-yaml";
import { getFileName } from "@foxkit/node-util/path";
import { slugify } from "modern-diacritics";
import { globby } from "globby";
import { TagsInfo, TagsInfoSchema } from "lib/schema/TagsInfo";
import { PostFrontmatterSchema } from "lib/schema/PostFrontmatter";
const processor = unified()
.use(remarkParse)
.use(remarkFrontmatter, ["yaml"])
.use(remarkExtractFrontmatter, { yaml: YAML.parse, remove: true })
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeStringify, { allowDangerousHtml: true });
const tagTitleMap = new Map<string, string>();
const tagDescriptionMap = new Map<string, string>();
const filePathMap = new Map<string, string>();
async function readTagsData() {
const data = await readFileYaml<TagsInfo>("data/tags.yml");
if (!data) {
throw new Error("Could not read 'data/tags.yml'");
}
const check = TagsInfoSchema.safeParse(data);
if (!check.success) {
check.error.errors.forEach(error =>
console.error(`${error.path}: ${error.message}`)
);
throw new Error("Stopping due to Schema validation error");
}
for (const [slug, { title, description }] of Object.entries(data)) {
tagTitleMap.set(slug, title);
tagDescriptionMap.set(slug, description);
}
}
async function processData(filePath: string) {
// readFile and rerun unified processor
const content = await readFile(filePath);
if (!content) {
throw new Error(`Could not read ${filePath}`);
}
const processed = await processor.process(content);
// process frontmatter
const slug = getFileName(filePath, false);
const check = PostFrontmatterSchema.safeParse(processed.data);
if (!check.success) {
check.error.errors.forEach(error =>
console.error(`${error.path}: ${error.message}`)
);
throw new Error("Stopping due to Schema validation error");
}
const data = check.data;
// check for slug collision
if (filePathMap.has(slug)) {
throw new Error(
`Slug collision for '${filePath}' and ${filePathMap.get(slug)}`
);
}
filePathMap.set(slug, filePath);
// transform tags
data.tags = (data.tags || []).map(text => {
const tagSlug = slugify(text);
const existingText = tagTitleMap.get(tagSlug);
if (existingText) {
if (existingText != text) {
throw new Error(
`Duplicate text for tag ${tagSlug}: '${text}' and ${existingText}`
);
}
} else {
tagTitleMap.set(tagSlug, text);
}
return tagSlug;
});
return { ...data, slug } as PostMeta;
}
(async function main() {
await readTagsData();
const postPaths = await globby("data/posts/**/*.md");
const posts = await Promise.all(postPaths.map(processData));
const tags = Object.fromEntries(
Array.from(tagTitleMap.entries(), ([slug, title]) => {
if (tagDescriptionMap.has(slug)) {
return [slug, { title, description: tagDescriptionMap.get(slug)! }];
}
return [slug, { title }];
})
);
posts.sort((a, b) => b.date - a.date);
await writeFile("posts.json", { posts, tags });
})();