| 1 | import { defineRouteMiddleware } from '@astrojs/starlight/route-data'; |
| 2 | import { ogVersion } from '@g1t/contracts/og'; |
| 3 | |
| 4 | import { TABS, type Tab, tabForGroup } from './tabs'; |
| 5 | |
| 6 | type Entry = { type: string; label?: string; href?: string; isCurrent?: boolean; entries?: Entry[] }; |
| 7 | |
| 8 | /** The social cards' service; see services/og. */ |
| 9 | const OG = 'https://og.g1t.sh'; |
| 10 | |
| 11 | /** The slug of a group's first page on this site, such as `reference/api`. */ |
| 12 | function firstSlug(entries: Entry[]): string | undefined { |
| 13 | for (const entry of entries) { |
| 14 | if (entry.type === 'group') { |
| 15 | const slug = firstSlug(entry.entries ?? []); |
| 16 | if (slug !== undefined) return slug; |
| 17 | } else if (entry.href?.startsWith('/') && !entry.href.startsWith('//')) { |
| 18 | return entry.href.replace(/^\/|\/$/g, ''); |
| 19 | } |
| 20 | } |
| 21 | return undefined; |
| 22 | } |
| 23 | |
| 24 | /** The label of the sidebar group holding the current page, the innermost if they nest. */ |
| 25 | function sectionOf(entries: Entry[], group?: string): string | undefined { |
| 26 | for (const entry of entries) { |
| 27 | if (entry.type === 'group') { |
| 28 | const found = sectionOf(entry.entries ?? [], entry.label); |
| 29 | if (found !== undefined) return found; |
| 30 | } else if (entry.isCurrent) { |
| 31 | return group; |
| 32 | } |
| 33 | } |
| 34 | return undefined; |
| 35 | } |
| 36 | |
| 37 | /** What the header and the page's title show: the tabs, and where the page sits. */ |
| 38 | export type Nav = { |
| 39 | tabs: { label: string; href: string; current: boolean }[]; |
| 40 | /** The sidebar group holding the page, or its tab's name, shown above its title. */ |
| 41 | section?: string; |
| 42 | }; |
| 43 | |
| 44 | /** Each route's Nav, set here for Header.astro and PageTitle.astro. */ |
| 45 | export const NAV = new WeakMap<object, Nav>(); |
| 46 | |
| 47 | /** |
| 48 | * The tab a top-level sidebar group belongs to. The API's groups are told |
| 49 | * apart by their pages, not their labels, since an API tag can share a |
| 50 | * guide group's name. |
| 51 | */ |
| 52 | function tabOf(group: Entry): Tab { |
| 53 | const slug = firstSlug(group.entries ?? []) ?? ''; |
| 54 | return slug.startsWith('reference/') ? TABS[TABS.length - 1] : tabForGroup(group.label ?? ''); |
| 55 | } |
| 56 | |
| 57 | /** Whether the current page is in these entries, at any depth. */ |
| 58 | function holdsCurrent(entries: Entry[]): boolean { |
| 59 | return entries.some((entry) => entry.isCurrent || holdsCurrent(entry.entries ?? [])); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Each page's sidebar holds only its tab's groups. |
| 64 | * |
| 65 | * Each page also gets its own social card: its title, its section and its |
| 66 | * description, drawn by the og service. |
| 67 | */ |
| 68 | export const onRequest = defineRouteMiddleware((context) => { |
| 69 | const route = context.locals.starlightRoute; |
| 70 | const groups = (route.sidebar as Entry[]).filter((entry) => entry.type === 'group'); |
| 71 | const section = sectionOf(groups); |
| 72 | // A page in no group, such as the home page, is in the first tab. |
| 73 | const holder = groups.find((group) => holdsCurrent(group.entries ?? [])); |
| 74 | const tab = holder ? tabOf(holder) : TABS[0]; |
| 75 | // A tab opens on the first page of its first group; the first tab, on the home page. |
| 76 | const tabs = TABS.map((each) => { |
| 77 | const group = each === TABS[0] ? undefined : groups.find((entry) => tabOf(entry) === each); |
| 78 | const slug = group ? firstSlug(group.entries ?? []) : undefined; |
| 79 | return { label: each.label, href: slug === undefined ? '/' : `/${slug}/`, current: each === tab }; |
| 80 | }); |
| 81 | NAV.set(route, { tabs, section: section ?? (route.id === '' ? undefined : tab.label) }); |
| 82 | route.sidebar = route.sidebar.filter((entry) => entry.type === 'group' && tabOf(entry as Entry) === tab); |
| 83 | |
| 84 | const { title, description } = route.entry.data; |
| 85 | const card = new URL('/docs', OG); |
| 86 | card.searchParams.set('title', title); |
| 87 | if (section) card.searchParams.set('section', section); |
| 88 | if (description) card.searchParams.set('description', description); |
| 89 | // The render version, so a new card design is a new address for every cache. |
| 90 | card.searchParams.set('v', ogVersion()); |
| 91 | const image = card.toString(); |
| 92 | const alt = `${title} · g1t docs`; |
| 93 | const meta = (key: 'property' | 'name', name: string, content: string) => ({ |
| 94 | tag: 'meta' as const, |
| 95 | attrs: { [key]: name, content }, |
| 96 | }); |
| 97 | // Starlight already writes og:title, og:description, og:url and |
| 98 | // twitter:card; these finish the card. |
| 99 | route.head.push( |
| 100 | meta('property', 'og:image', image), |
| 101 | meta('property', 'og:image:type', 'image/png'), |
| 102 | meta('property', 'og:image:width', '1200'), |
| 103 | meta('property', 'og:image:height', '630'), |
| 104 | meta('property', 'og:image:alt', alt), |
| 105 | meta('name', 'twitter:title', title), |
| 106 | meta('name', 'twitter:image', image), |
| 107 | meta('name', 'twitter:image:alt', alt), |
| 108 | ); |
| 109 | if (description) route.head.push(meta('name', 'twitter:description', description)); |
| 110 | }); |