@@ -0,0 +1,110 @@
1+const fs = require("fs");
2+const path = require("path");
3+const matter = require("gray-matter");
4+const toml = require("@iarna/toml");
6+// node ./buildPageTree.js [directory] [sort]
7+// eg. node ./buildPageTree docs weight
8+// will produce docs.json in the cache folder.
12+ toml: toml.parse.bind(toml),
19+ * buildPageTree is written to provide a recursive index of posts
20+ * for nested directories for creating sidebars
21+ * @param {string} path Absolute path to the subdirectory.
22+ * @returns {Object.<string,string|Object<string,string[]|number>>} Title of the directory, child pages, and child directories (recursive structure).
34+// children: { [string]: PageTree },
37+function buildPageTree(dirPath, ordering = "", content = false) {
38+ const metadata = matter(
39+ fs.readFileSync(path.join(dirPath, "_index.md")),
42+ // get a list of contents at the dirPath, with file types so that the item at the dirPath can be identified as file or folder
43+ const children = fs.readdirSync(dirPath, { withFileTypes: true });
44+ // get a list of files
45+ const pages = children.filter((f) => f.isFile() && f.name !== "_index.md");
46+ // get a list of folders
47+ const subdirs = children.filter((f) => f.isDirectory());
48+ // return a pagetree datastructure
50+ title: metadata.data.title,
53+ // retrieve topmatter as JSON
54+ const { content, data } = matter(
55+ fs.readFileSync(path.join(dirPath, page.name)),
62+ slug: page.name.replace(/.md$/, ""),
63+ ...(ordering === "weight" && { weight: data?.weight ?? 0 }),
64+ ...(ordering === "date" && {
65+ date: data?.date ?? "2000-01-01T00:00:00.000Z",
69+ .sort((a, b) => a.weight - b.weight),
70+ children: Object.fromEntries(
73+ const aMetadata = matter(
74+ fs.readFileSync(path.join(dirPath, a.name, "_index.md")),
77+ const bMetadata = matter(
78+ fs.readFileSync(path.join(dirPath, b.name, "_index.md")),
81+ return aMetadata?.data?.weight - bMetadata?.data?.weight || 0;
85+ buildPageTree(path.join(dirPath, subdir.name), ordering, content),
91+const process = require("process");
93+const arg = process.argv?.[2];
94+const sort = process?.argv?.[3] || "weight";
96+const dir = buildPageTree(path.join(process.cwd(), `content/${arg}`), sort);
97+const fileContents = `${JSON.stringify(dir)}`;
100+ fs.readdirSync("cache");
102+ fs.mkdirSync("cache");
105+fs.writeFile(`cache/${arg}.json`, fileContents, function (err) {
106+ if (err) return console.error(err);
107+ console.log(`${arg} directory structure created.`);
110+module.exports = { buildPageTree };