forked from glitchassassin/screeps-wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-router.config.ts
42 lines (36 loc) · 1.19 KB
/
react-router.config.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
import type { Config } from "@react-router/dev/config";
import fs from "node:fs/promises";
import path from "node:path";
import { parse as parseYaml } from "yaml";
export default {
ssr: true,
prerender: async ({ getStaticPaths }) => {
const paths = getStaticPaths();
const wikiDir = path.join(process.cwd(), "app/routes/_base+/_wiki+");
const files = await fs.readdir(wikiDir);
const mdFiles = files.filter(
(file) => file.endsWith(".md") || file.endsWith(".mdx")
);
const categories = new Set<string>();
for (const file of mdFiles) {
const content = await fs.readFile(path.join(wikiDir, file), "utf-8");
const match = content.match(/^---\n([\s\S]*?)\n---/);
if (match) {
try {
const frontmatter = parseYaml(match[1]);
if (frontmatter.categories) {
frontmatter.categories.forEach((category: string) =>
categories.add(category)
);
}
} catch (e) {
console.warn(`Failed to parse frontmatter in ${file}:`, e);
}
}
}
for (const category of categories) {
paths.push(`/Categories/${category}`);
}
return paths;
},
} satisfies Config;