@@ -162,42 +162,50 @@ function set(obj, path, value) {
165− * getLinksinSubdirs is written to provide a recursive index of posts
165+ * buildPageTree is written to provide a recursive index of posts
166− * for nested directories for creating sidebars.
166+ * for nested directories for creating sidebars
167 * @param {string} path Absolute path to the subdirectory.
167 * @param {string} path Absolute path to the subdirectory.
168− * @returns {object} Nested object by directory with name of directory,
168+ * @returns {Object.<string,string|Object<string,string[]|number>>} Title of the directory, child pages, and child directories (recursive structure).
169− * child pages (as an array of objects with title, .md filename, slug, and weight),
169+ *
170− * and subdirectories (named simply by the folder name). Iterate through the object
180+// children: { [string]: PageTree },
174−export function getLinksinSubdirs(path) {
183+export function buildPageTree(path) {
175− const allFiles = getAllFiles(path);
184+ const metadata = matter(fs.readFileSync(join(path, "_index.md")), options);
176− const fileTree = {};
185+
177− allFiles.forEach((e) => {
186+ const children = fs.readdirSync(path, { withFileTypes: true });
178− const parsed = parse(e.substr(e.indexOf("content/") + 7));
187+ const pages = children.filter((f) => f.isFile() && f.name !== "_index.md");
179− const keys = parsed.dir.split("/").filter((e) => e !== "");
188+ const subdirs = children.filter((f) => f.isDirectory());
180− if (!get(fileTree, keys, false)) {
189+
181− const fullPathofDir = join(
190+ return {
182− process.cwd(),
191+ title: metadata.data.title,
183− "/content",
192+ pages: pages.map((page) => {
184− parsed.dir,
193+ const { data: pageData } = matter(
185− "_index.md"
194+ fs.readFileSync(join(path, page.name)),
187− const dirContents = fs.readFileSync(fullPathofDir, "utf8");
197+ return {
188− const { data: dirData } = matter(dirContents, options);
198+ title: pageData.title,
189− set(fileTree, keys, { title: dirData.title, pages: [] });
199+ base: page.name,
190− }
200+ slug: page.name.replace(/.md$/, ""),
191− const fileContents = fs.readFileSync(e, "utf8");
192− const { data } = matter(fileContents, options);
193− if (!e.includes("_index.md")) {
194− get(fileTree, keys, false).pages.push({
198− weight: data?.weight ?? 0,
201+ weight: pageData?.weight ?? 0,
201− });
204+ children: Object.fromEntries(
202− return fileTree;
205+ subdirs.map((subdir) => [
207+ buildPageTree(join(path, subdir.name)),