From 8066b04b36d9bcd5fa83b10ec5da9c6e05c225a0 Mon Sep 17 00:00:00 2001 From: Drew Haven Date: Thu, 7 May 2026 17:01:39 -0700 Subject: [PATCH] Capitalize categories in hierarchy --- src/components/NoteHierarchy.astro | 3 ++- src/lib/hierarchy.ts | 17 +++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) 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;