@@ -0,0 +1,69 @@
1+const fs = require("fs");
2+const path = require("path");
3+const matter = require("gray-matter");
4+const toml = require("@iarna/toml");
5+
6+const options = {
7+ engines: {
8+ toml: toml.parse.bind(toml),
9+ },
10+ language: "toml",
11+ delimiters: "+++",
12+};
13+
14+const index = [];
15+
16+function buildSearchIndex(dir) {
17+ const dirName = dir.split("/").slice(-1).join("");
18+ let metadata = {
19+ data: {
20+ title: dirName.charAt(0).toUpperCase() + dirName.slice(1),
21+ },
22+ };
23+
24+ try {
25+ metadata = matter(fs.readFileSync(path.join(dir, "_index.md")), options);
26+ } catch (err) {}
27+
28+ const children = fs.readdirSync(dir, { withFileTypes: true });
29+ const pages = children.filter((f) => f.isFile() && f.name !== "_index.md");
30+ const subdirs = children.filter((f) => f.isDirectory());
31+
32+ index.push(
33+ ...pages.map((page) => {
34+ const { data: pageData, content: pageContent } = matter(
35+ fs.readFileSync(path.join(dir, page.name)),
36+ options
37+ );
38+ return {
39+ title: pageData.title,
40+ base: page.name,
41+ slug: page.name.replace(/.md$/, ""),
42+ content: pageContent,
43+ parent: metadata.data.title,
44+ };
45+ })
46+ );
47+
48+ Object.fromEntries(
49+ subdirs.map((subdir) => [
50+ subdir.name,
51+ buildSearchIndex(path.join(dir, subdir.name)),
52+ ])
53+ );
54+}
55+
56+buildSearchIndex(path.join(process.cwd(), "content"));
57+
58+const fileContents = `export const index = ${JSON.stringify(index)}`;
59+
60+try {
61+ fs.readdirSync("cache");
62+} catch (err) {
63+ fs.mkdirSync("cache");
64+}
65+
66+fs.writeFile("cache/data.js", fileContents, function (err) {
67+ if (err) return console.error(err);
68+ console.log("Site index created.");
69+});