Implementation in Starlight
Create TagList
component
import { getCollection } from " astro:content " ;
const docs = await getCollection ( " docs " );
const docsByTags = new Map < string , Docs > ();
doc . data . tags . forEach ( ( tag : string ) => {
docsByTags . set (tag, docsByTags . get (tag) || []);
docsByTags . get (tag) ?. push (doc);
const comparator = new Intl . Collator ( " en " );
const tagsSorted = [ ... docsByTags . keys ()] . sort (comparator . compare );
tagsSorted . map ( ( tag ) => (
{ tag } ( { docsByTags . get (tag) ?. length } )
{ docsByTags . get (tag) ?. map ( ( doc ) => (
< a href = { ` / ${ doc . slug } ` } > { doc . data . title } </ a >
Adjust content schema
import { z, defineCollection } from " astro:content " ;
import { docsSchema, i18nSchema } from " @astrojs/starlight/schema " ;
export const collections = {
tags: z . array (z . string ()) . optional () ,
i18n: defineCollection ( { type: " data " , schema: i18nSchema () } ) ,
Use component , wherever you like
import TagList from " @components/TagList.astro " ;
Example
See Tags .
Further improvements
Tag page
From starlight-blog β :
Each tag can have itβs own color or icon , to help differentiate them visually, for example: #markdown , #component , #link , #page , #Astro , #TypeScript