From 5786fd827fef80fc952ff72549a080d15ce3f6da Mon Sep 17 00:00:00 2001 From: brutsalvadi Date: Thu, 23 Apr 2026 09:44:50 +0200 Subject: [PATCH] Fix bonsai tree build and add entries index page Fix buildBonsai split error: Astro 6 githubSlug transforms 'i.bonsai' to 'ibonsai' breaking the semtree root lookup. Added custom generateId to the index collection and switched to doc.id directly as bonsaiText key. Move buildBonsai from module-level await to per-component call so it runs after the content store is ready. Add /entries index page listing all wiki pages. --- src/components/Branch.astro | 3 ++- src/components/BreadCrumbs.astro | 3 ++- src/content.config.ts | 6 +++++- src/pages/entries/index.astro | 33 ++++++++++++++++++++++++++++++++ src/pages/map.astro | 3 ++- src/wikibonsai/semtree.ts | 20 ++++--------------- 6 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 src/pages/entries/index.astro diff --git a/src/components/Branch.astro b/src/components/Branch.astro index 6fb8740..65d6f20 100644 --- a/src/components/Branch.astro +++ b/src/components/Branch.astro @@ -1,10 +1,11 @@ --- import { getCollection } from 'astro:content'; import { SITE_TITLE } from '../consts'; -import { bonsai } from '../wikibonsai/semtree'; +import { buildBonsai } from '../wikibonsai/semtree'; const { nodes } = Astro.props; +const bonsai = await buildBonsai(); const root = bonsai ? bonsai.root : ''; const treeNodes = bonsai ? bonsai.nodes : []; --- diff --git a/src/components/BreadCrumbs.astro b/src/components/BreadCrumbs.astro index 130d3b5..15be13e 100644 --- a/src/components/BreadCrumbs.astro +++ b/src/components/BreadCrumbs.astro @@ -1,9 +1,10 @@ --- import { SITE_TITLE } from '../consts'; -import { bonsai } from '../wikibonsai/semtree'; +import { buildBonsai } from '../wikibonsai/semtree'; const { current } = Astro.props; +const bonsai = await buildBonsai(); const root = bonsai ? bonsai.root : ''; const treeNodes = bonsai ? bonsai.nodes : []; diff --git a/src/content.config.ts b/src/content.config.ts index 78dd7cd..0b5e838 100644 --- a/src/content.config.ts +++ b/src/content.config.ts @@ -26,7 +26,11 @@ const entries = defineCollection({ }); const index = defineCollection({ - loader: glob({ pattern: '**/*.md', base: './src/content/index' }), + loader: glob({ + pattern: '**/*.md', + base: './src/content/index', + generateId: ({ entry }) => entry.replace(/\.md$/, ''), + }), schema: z.object({ title: z.string(), }), diff --git a/src/pages/entries/index.astro b/src/pages/entries/index.astro new file mode 100644 index 0000000..309aa6c --- /dev/null +++ b/src/pages/entries/index.astro @@ -0,0 +1,33 @@ +--- +import Base from '../../layouts/Base.astro'; +import { SITE_TITLE, SITE_DESCRIPTION } from '../../consts'; +import { getCollection } from 'astro:content'; + +const entries = (await getCollection('entries')) + .sort((a, b) => (b.data.updatedDate ?? b.data.pubDate ?? new Date(0)).valueOf() + - (a.data.updatedDate ?? a.data.pubDate ?? new Date(0)).valueOf()); +--- + + +
+

All Pages

+ {entries.length === 0 && ( +

No pages yet.

+ )} + +
+ diff --git a/src/pages/map.astro b/src/pages/map.astro index d8243c9..142b851 100644 --- a/src/pages/map.astro +++ b/src/pages/map.astro @@ -2,8 +2,9 @@ import Base from '../layouts/Base.astro'; import Branch from '../components/Branch.astro'; import { SITE_TITLE, SITE_DESCRIPTION } from '../consts'; -import { bonsai } from '../wikibonsai/semtree'; +import { buildBonsai } from '../wikibonsai/semtree'; +const bonsai = await buildBonsai(); const root = bonsai ? bonsai.root : ''; const treeNodes = bonsai ? bonsai.nodes : []; --- diff --git a/src/wikibonsai/semtree.ts b/src/wikibonsai/semtree.ts index 212759d..3290d0b 100644 --- a/src/wikibonsai/semtree.ts +++ b/src/wikibonsai/semtree.ts @@ -16,8 +16,8 @@ export async function buildBonsai(): Promise { const rootFilename: string = 'i.bonsai'; // build 'bonsaiText' hash const allIndexDocs = await getCollection('index'); - allIndexDocs.forEach((doc: any) => { // remove preceding/trailing newlines/whitespace - bonsaiText[path.basename(doc.id, '.md')] = doc.body.replace(/^\s+|\s+$/g, ''); + allIndexDocs.forEach((doc: any) => { + bonsaiText[doc.id] = (doc.body ?? '').replace(/^\s+|\s+$/g, ''); }); let bonsai: SemTree | string = 'uninitialized bonsai'; try { @@ -49,19 +49,6 @@ export async function buildBonsai(): Promise { // node.url = '/blog/' + doc.slug; // } // } - console.log('bonsai: \n' - + '\n---\n' - + 'root: ' + bonsai.root - + '\n---\n' - + 'branches: ' + bonsai.branches - + '\n---\n' - + 'petioleMap: ' + JSON.stringify(bonsai.petioleMap) - + '\n---\n' - + 'orphans: ' + bonsai.orphans - + '\n---\n' - + 'nodes: ' + JSON.stringify(bonsai.nodes) - + '\n---\n' - ); return bonsai; } } catch (e) { @@ -69,4 +56,5 @@ export async function buildBonsai(): Promise { } } -export const bonsai: SemTree | undefined = await buildBonsai(); +// Do not call buildBonsai() at module init — the content store isn't ready yet. +// Each component that needs the tree should call buildBonsai() directly.