pr_01m47d15m3e54sn21z27rpy5n9/apps/docs/src/route-data.ts
| 1 | import { defineRouteMiddleware } from '@astrojs/starlight/route-data'; |
| 2 | |
| 3 | import { tabFor } from './tabs'; |
| 4 | |
| 5 | type Entry = { type: string; label?: string; href?: string; isCurrent?: boolean; entries?: Entry[] }; |
| 6 | |
| 7 | /** The social cards' service; see services/og. */ |
| 8 | const OG = 'https://og.g1t.sh'; |
| 9 | |
| 10 | /** The slug of a group's first page on this site, such as `reference/api`. */ |
| 11 | function firstSlug(entries: Entry[]): string | undefined { |
| 12 | for (const entry of entries) { |
| 13 | if (entry.type === 'group') { |
| 14 | const slug = firstSlug(entry.entries ?? []); |
| 15 | if (slug !== undefined) return slug; |
| 16 | } else if (entry.href?.startsWith('/') && !entry.href.startsWith('//')) { |
| 17 | return entry.href.replace(/^\/|\/$/g, ''); |
| 18 | } |
| 19 | } |
| 20 | return undefined; |
| 21 | } |
| 22 | |
| 23 | /** The label of the sidebar group holding the current page, the innermost if they nest. */ |
| 24 | function sectionOf(entries: Entry[], group?: string): string | undefined { |
| 25 | for (const entry of entries) { |
| 26 | if (entry.type === 'group') { |
| 27 | const found = sectionOf(entry.entries ?? [], entry.label); |
| 28 | if (found !== undefined) return found; |
| 29 | } else if (entry.isCurrent) { |
| 30 | return group; |
| 31 | } |
| 32 | } |
| 33 | return undefined; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Each page's sidebar holds only its tab's groups: those whose pages |
| 38 | * belong to the same tab. Groups are told apart by their pages, not their |
| 39 | * labels, since a guide group and an API group can share a name. |
| 40 | * |
| 41 | * Each page also gets its own social card: its title, its section and its |
| 42 | * description, drawn by the og service. |
| 43 | */ |
| 44 | export const onRequest = defineRouteMiddleware((context) => { |
| 45 | const route = context.locals.starlightRoute; |
| 46 | const section = sectionOf(route.sidebar as Entry[]); |
| 47 | const tab = tabFor(route.id); |
| 48 | route.sidebar = route.sidebar.filter((entry) => { |
| 49 | if (entry.type !== 'group') return false; |
| 50 | const slug = firstSlug(entry.entries as Entry[]); |
| 51 | return slug !== undefined && tabFor(slug) === tab; |
| 52 | }); |
| 53 | |
| 54 | const { title, description } = route.entry.data; |
| 55 | const card = new URL('/docs', OG); |
| 56 | card.searchParams.set('title', title); |
| 57 | if (section) card.searchParams.set('section', section); |
| 58 | if (description) card.searchParams.set('description', description); |
| 59 | const image = card.toString(); |
| 60 | const alt = `${title} · g1t docs`; |
| 61 | const meta = (key: 'property' | 'name', name: string, content: string) => ({ |
| 62 | tag: 'meta' as const, |
| 63 | attrs: { [key]: name, content }, |
| 64 | }); |
| 65 | // Starlight already writes og:title, og:description, og:url and |
| 66 | // twitter:card; these finish the card. |
| 67 | route.head.push( |
| 68 | meta('property', 'og:image', image), |
| 69 | meta('property', 'og:image:type', 'image/png'), |
| 70 | meta('property', 'og:image:width', '1200'), |
| 71 | meta('property', 'og:image:height', '630'), |
| 72 | meta('property', 'og:image:alt', alt), |
| 73 | meta('name', 'twitter:title', title), |
| 74 | meta('name', 'twitter:image', image), |
| 75 | meta('name', 'twitter:image:alt', alt), |
| 76 | ); |
| 77 | if (description) route.head.push(meta('name', 'twitter:description', description)); |
| 78 | }); |