Alphabetical index
aka glossary
Implementation
-
Create
Alphabetical
componentsrc/components/Alphabetical.astro ---import { getCollection } from "astro:content";const firstChar = (str: string) => String.fromCodePoint(str.codePointAt(0)!);const docs = await getCollection("docs");type Docs = typeof docs;const docsByChar = new Map<string, Docs>();docs.forEach((doc) => {const char = firstChar(doc.data.title).toUpperCase();docsByChar.set(char, docsByChar.get(char) || []);docsByChar.get(char)?.push(doc);});const comparator = new Intl.Collator("en");const charsSorted = [...docsByChar.keys()].sort(comparator.compare);---{charsSorted.map((char) => (<><h3>{char}</h3><ul>{docsByChar.get(char)?.map((doc) => (<li><a href={`/${doc.slug}`}>{doc.data.title}</a></li>))}</ul></>))} -
Use component, wherever you like
---title: Alphabetical indextableOfContents: falseprev: falsenext: false---import Alphabetical from "@components/Alphabetical.astro";<Alphabetical />
Example
See Alphabetical index.