import { html, unsafeHTML } from '@elenajs/core'; import { LitroPage } from '@beatzball/litro/adapter/elena/page'; import { definePageData } from '@beatzball/litro'; import { getPosts } from 'litro:content'; import { siteConfig } from '../../server/starlight.config.js'; import { starlightHead } from '../../src/route-meta.js'; // Re-exporting child components prevents Rollup tree-shaking. export { StarlightPage } from '../../src/components/starlight-page.js'; /** * The /docs landing page. * * Without it, /docs is a 404 — only /docs/ exists — and a reader who * types the obvious path, or trims a URL back one segment, lands on nothing. * The page is prerendered like every other static route, so it works with no * client JavaScript. */ interface DocsIndexItem { label: string; href: string; description: string | null; } interface DocsIndexGroup { label: string; items: DocsIndexItem[]; } export interface DocsIndexData { groups: DocsIndexGroup[]; sidebar: typeof siteConfig.sidebar; siteTitle: string; nav: typeof siteConfig.nav; } export const pageData = definePageData(async (_event) => { const contentPrefix = '/content/docs/'; const posts = await getPosts(); // Each doc's frontmatter `description`, keyed by slug. const descriptions = new Map(); for (const post of posts) { if (!post.url.startsWith(contentPrefix)) continue; const description = post.description?.trim(); if (description) descriptions.set(post.url.slice(contentPrefix.length), description); } // A group with an empty `items` array is dropped: rendering it would put a // heading over an empty list. const groups = siteConfig.sidebar .map(group => ({ label: group.label, items: group.items.map(item => ({ label: item.label, href: `/docs/${item.slug}`, description: descriptions.get(item.slug) ?? null, })), })) .filter(group => group.items.length > 0); return { groups, sidebar: siteConfig.sidebar, siteTitle: siteConfig.title, nav: siteConfig.nav, } satisfies DocsIndexData; }); export const routeMeta = { head: starlightHead, title: 'Documentation — {{projectName}}', }; export class DocsIndexPage extends LitroPage { static override tagName = 'page-docs'; render() { const data = this.serverData as DocsIndexData | null; if (!data) return html`

Loading…

`; const { groups, sidebar, siteTitle, nav } = data; const groupsHtml = groups.map(group => `

${group.label}

    ${group.items.map(item => { const desc = item.description ? `${item.description}` : ''; return `
  • ${item.label}${desc}
  • `; }).join('')}
`).join(''); return html` ${unsafeHTML(groupsHtml)} `; } } DocsIndexPage.define(); export default DocsIndexPage;