diff --git a/src/components/NoteHierarchy.astro b/src/components/NoteHierarchy.astro index 48a61aa..76f9e41 100644 --- a/src/components/NoteHierarchy.astro +++ b/src/components/NoteHierarchy.astro @@ -10,8 +10,9 @@ interface Props { const { prefix, hierarchy: maybeHierarchy} = Astro.props; -const hierarchy = maybeHierarchy ?? makeHierarchy((await getCollection('notes')).map(note => [note.id, note.data.title])); +const notes = await getCollection('notes'); +const hierarchy = maybeHierarchy ?? makeHierarchy(notes.map(note => [note.id, note.data.title])); const pathname = Astro.url.pathname.replace(import.meta.env.BASE_URL, '/'); diff --git a/src/lib/hierarchy.ts b/src/lib/hierarchy.ts index 44a0d5a..0bf89bd 100644 --- a/src/lib/hierarchy.ts +++ b/src/lib/hierarchy.ts @@ -14,17 +14,18 @@ function addToHierarchy( return tree; } - if (!tree[path[0]]) { - tree[path[0]] = { id: null, title: null, children: null }; - } - let curr = tree[path[0]]; + // Create a dummy node to start the loop since the Hierarchy root is not a node. + let curr: HierarchyNode = { id: null, title: null, children: tree }; + + for (const node of path) { + // Capitalize first letter + const nodeName = node.charAt(0).toUpperCase() + node.slice(1); - for (const node of path.slice(1)) { curr.children ||= {}; - if (!curr.children[node]) { - curr.children[node] = { id: null, title: null, children: null }; + if (!curr.children[nodeName]) { + curr.children[nodeName] = { id: null, title: null, children: null }; } - curr = curr.children[node]; + curr = curr.children[nodeName]; } curr.id = noteId;