@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 93 lines 3.13 KB
Raw
1 /// <reference types="@astrojs/starlight/locals" />
2 import { defineRouteMiddleware } from "@astrojs/starlight/route-data";
3 import type { StarlightRouteData } from "@astrojs/starlight/route-data";
4 import { getCollection } from "astro:content";
5 import type { BlogCategory } from "./content.config";
6
7 type SidebarItem = StarlightRouteData["sidebar"][number];
8 type SidebarGroup = Extract<SidebarItem, { type: "group" }>;
9
10 const groupLabels: Record<BlogCategory, string> = {
11 release: "Releases",
12 post: "Posts",
13 };
14 const groupOrder: BlogCategory[] = ["post", "release"];
15
16 interface BlogMeta {
17 category: BlogCategory;
18 date: Date | undefined;
19 }
20
21 let metaById: Map<string, BlogMeta> | undefined;
22
23 async function loadMetaById(): Promise<Map<string, BlogMeta>> {
24 if (metaById) return metaById;
25 const entries = await getCollection("docs", (entry) => entry.id.startsWith("blog/"));
26 const map = new Map<string, BlogMeta>();
27 for (const entry of entries) {
28 const data = entry.data as { category?: BlogCategory; date?: Date };
29 map.set(entry.id, { category: data.category ?? "post", date: data.date });
30 }
31 metaById = map;
32 return map;
33 }
34
35 // Release titles lead with the version ("2.0.0-beta.80 - GCP, …"), which
36 // truncates the descriptive part in the narrow sidebar. Lead with the
37 // release date instead; the page itself keeps the versioned title.
38 function releaseLabel(label: string, date: Date | undefined): string {
39 if (!date) return label;
40 const title = label.replace(/^\S+\s+-\s+/, "");
41 return `${date.toISOString().slice(0, 10)} · ${title}`;
42 }
43
44 function extractBlogId(href: string): string | undefined {
45 const match = href.match(/\/blog\/([^/]+)\/?$/);
46 return match ? `blog/${match[1]}` : undefined;
47 }
48
49 export const onRequest = defineRouteMiddleware(async (context, next) => {
50 await next();
51
52 // TODO: fix types
53 const { starlightRoute, t } = context.locals as Record<string, any>;
54 const recentLabel = t("starlightBlog.sidebar.recent");
55
56 const recentIndex = starlightRoute.sidebar.findIndex(
57 (item: any): item is SidebarGroup => item.type === "group" && item.label === recentLabel,
58 );
59 if (recentIndex === -1) return;
60
61 const recentGroup = starlightRoute.sidebar[recentIndex] as SidebarGroup;
62 const metas = await loadMetaById();
63
64 const buckets = new Map<BlogCategory, SidebarItem[]>();
65 for (const category of groupOrder) buckets.set(category, []);
66
67 for (const item of recentGroup.entries) {
68 if (item.type !== "link") continue;
69 const id = extractBlogId(item.href);
70 const meta = id !== undefined ? metas.get(id) : undefined;
71 const category: BlogCategory = meta?.category ?? "post";
72 buckets
73 .get(category)!
74 .push(
75 category === "release" ? { ...item, label: releaseLabel(item.label, meta?.date) } : item,
76 );
77 }
78
79 const replacement: SidebarGroup[] = [];
80 for (const category of groupOrder) {
81 const entries = buckets.get(category)!;
82 if (entries.length === 0) continue;
83 replacement.push({
84 type: "group",
85 label: groupLabels[category],
86 entries,
87 collapsed: false,
88 badge: undefined,
89 });
90 }
91
92 starlightRoute.sidebar.splice(recentIndex, 1, ...replacement);
93 });