@@ -139,13 +139,35 @@ export const getAllFiles = function (dirPath, arrayOfFiles) {
139139 return arrayOfFiles;
140140};
141141
142+function get(obj, path, defaultValue) {
143+ for (const index of path) {
144+ if (obj[index] === undefined) {
145+ return defaultValue;
146+ }
147+ obj = obj[index];
148+ }
149+ return obj;
150+}
151+
152+function set(obj, path, value) {
153+ let piece = obj;
154+ for (let i = 0; i < path.length - 1; i++) {
155+ if (piece[path[i]] === undefined) {
156+ piece[path[i]] = typeof path[i + 1] === "number" ? [] : {};
157+ }
158+ piece = piece[path[i]];
159+ }
160+ piece[path[path.length - 1]] = value;
161+ return obj;
162+}
163+
142164export function getLinksinSubdirs(path) {
143165 const allFiles = getAllFiles(path);
144166 const fileTree = {};
145167 allFiles.forEach((e) => {
146168 const parsed = parse(e.substr(e.indexOf("content/") + 7));
147−
148− if (!(parsed.dir in fileTree)) {
169+ const keys = parsed.dir.split("/").filter((e) => e !== "");
170+ if (!get(fileTree, keys, false)) {
149171 const fullPathofDir = join(
150172 process.cwd(),
151173 "/content",
@@ -154,15 +176,15 @@ export function getLinksinSubdirs(path) {
154176 );
155177 const dirContents = fs.readFileSync(fullPathofDir, "utf8");
156178 const { data: dirData } = matter(dirContents, options);
157− fileTree[parsed.dir] = { name: dirData.title, pages: [] };
179+ set(fileTree, keys, { title: dirData.title, pages: [] });
158180 }
159−
160181 const fileContents = fs.readFileSync(e, "utf8");
161182 const { data } = matter(fileContents, options);
162− fileTree[parsed.dir].pages.push({
163− name: data.title,
183+ get(fileTree, keys, false).pages.push({
184+ title: data.title,
164185 base: parsed.base,
165186 slug: parsed.name,
187+ weight: data?.weight ?? 0,
166188 });
167189 });
168190 return fileTree;