Capitalize categories in hierarchy

This commit is contained in:
2026-05-07 17:01:39 -07:00
parent 774c713110
commit 8066b04b36
2 changed files with 11 additions and 9 deletions

View File

@@ -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, '/');

View File

@@ -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;