|
| 1 | +import { findPackage, packages } from "./packages.ts"; |
| 2 | +import type { PackageIconName } from "./package-icons.ts"; |
| 3 | +import type { PackageInfo } from "./packages.ts"; |
| 4 | + |
| 5 | +/** An element of the `<head>` of a page, as Starlight renders it. */ |
| 6 | +interface HeadEntry { |
| 7 | + readonly tag: "meta" | "script"; |
| 8 | + readonly attrs: Readonly<Record<string, string>>; |
| 9 | + readonly content?: string; |
| 10 | +} |
| 11 | + |
| 12 | +/** What a page tells search engines and link previews about itself. */ |
| 13 | +interface PageInfo { |
| 14 | + /** The URL of the docs' home page, ending with a slash. */ |
| 15 | + readonly siteUrl: string; |
| 16 | + readonly pageUrl: string; |
| 17 | + readonly siteTitle: string; |
| 18 | + readonly title: string; |
| 19 | + readonly description: string; |
| 20 | + /** The first segment of the page's path, which names its package, if any. */ |
| 21 | + readonly folder: string; |
| 22 | +} |
| 23 | + |
| 24 | +/** An Open Graph image in `public`, which matches its text alternative. */ |
| 25 | +interface OpenGraphImage { |
| 26 | + readonly path: string; |
| 27 | + readonly alt: string; |
| 28 | +} |
| 29 | + |
| 30 | +/** A node of schema.org data. */ |
| 31 | +type LinkedData = Readonly<Record<string, unknown>>; |
| 32 | + |
| 33 | +/** A node of schema.org data that other nodes refer to by its `@id`. */ |
| 34 | +interface LinkedNode extends LinkedData { |
| 35 | + readonly "@id": string; |
| 36 | +} |
| 37 | + |
| 38 | +const landingImage: OpenGraphImage = { |
| 39 | + alt: "lynstack recipe: variants in, styles out. Fast, type-safe recipes that map a component's variants to its class names, its React Native styles, or values of any type.", |
| 40 | + path: "og.png", |
| 41 | +}; |
| 42 | + |
| 43 | +const packageImages = { |
| 44 | + "class-recipe": { |
| 45 | + alt: "lynstack class-recipe: variants in, class names out. cva, sva, and cx, a drop-in replacement for clsx.", |
| 46 | + path: "og/class-recipe.png", |
| 47 | + }, |
| 48 | + "native-recipe": { |
| 49 | + alt: "lynstack native-recipe: variants in, styles out. React Native style recipes that return the same frozen style for the same variants.", |
| 50 | + path: "og/native-recipe.png", |
| 51 | + }, |
| 52 | + recipe: { |
| 53 | + alt: "lynstack recipe: variants in, values out. The engine for recipes of any type.", |
| 54 | + path: "og/recipe.png", |
| 55 | + }, |
| 56 | +} as const satisfies Readonly<Record<PackageIconName, OpenGraphImage>>; |
| 57 | + |
| 58 | +const imagesByFolder: ReadonlyMap<string, OpenGraphImage> = new Map( |
| 59 | + Object.entries(packageImages), |
| 60 | +); |
| 61 | + |
| 62 | +function openGraphProperty(property: string, content: string): HeadEntry { |
| 63 | + return { attrs: { content, property }, tag: "meta" }; |
| 64 | +} |
| 65 | + |
| 66 | +function openGraphImageTags(siteUrl: string, folder: string): HeadEntry[] { |
| 67 | + const image = imagesByFolder.get(folder) ?? landingImage; |
| 68 | + return [ |
| 69 | + openGraphProperty("og:image", `${siteUrl}${image.path}`), |
| 70 | + openGraphProperty("og:image:width", "1200"), |
| 71 | + openGraphProperty("og:image:height", "630"), |
| 72 | + openGraphProperty("og:image:alt", image.alt), |
| 73 | + ]; |
| 74 | +} |
| 75 | + |
| 76 | +function packageNode( |
| 77 | + siteUrl: string, |
| 78 | + folder: string, |
| 79 | + pkg: PackageInfo, |
| 80 | +): LinkedNode { |
| 81 | + const url = `${siteUrl}${folder}/`; |
| 82 | + return { |
| 83 | + "@id": `${url}#package`, |
| 84 | + "@type": "SoftwareSourceCode", |
| 85 | + codeRepository: pkg.source, |
| 86 | + description: pkg.summary, |
| 87 | + license: `https://spdx.org/licenses/${pkg.license}.html`, |
| 88 | + name: pkg.name, |
| 89 | + programmingLanguage: "TypeScript", |
| 90 | + url, |
| 91 | + version: pkg.version, |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Describes the site, the page, and the packages it is about as schema.org |
| 97 | + * data: the landing page is a `WebPage` about every package, and a page of |
| 98 | + * a package is a `TechArticle` about that package. |
| 99 | + */ |
| 100 | +function structuredData(page: PageInfo): LinkedData { |
| 101 | + const websiteId = `${page.siteUrl}#website`; |
| 102 | + const pkg = findPackage(page.folder); |
| 103 | + const about = pkg |
| 104 | + ? [packageNode(page.siteUrl, page.folder, pkg)] |
| 105 | + : Object.entries(packages).map( |
| 106 | + ([folder, info]: readonly [string, PackageInfo]) => |
| 107 | + packageNode(page.siteUrl, folder, info), |
| 108 | + ); |
| 109 | + return { |
| 110 | + "@context": "https://schema.org", |
| 111 | + "@graph": [ |
| 112 | + { |
| 113 | + "@id": websiteId, |
| 114 | + "@type": "WebSite", |
| 115 | + inLanguage: "en", |
| 116 | + name: page.siteTitle, |
| 117 | + url: page.siteUrl, |
| 118 | + }, |
| 119 | + ...about, |
| 120 | + { |
| 121 | + "@id": page.pageUrl, |
| 122 | + "@type": pkg ? "TechArticle" : "WebPage", |
| 123 | + about: about.map((node) => ({ "@id": node["@id"] })), |
| 124 | + description: page.description, |
| 125 | + headline: page.title, |
| 126 | + inLanguage: "en", |
| 127 | + isPartOf: { "@id": websiteId }, |
| 128 | + url: page.pageUrl, |
| 129 | + }, |
| 130 | + ], |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | +/** Serializes `data` for a `<script>`, which must not contain `</script>`. */ |
| 135 | +function jsonLd(data: LinkedData): HeadEntry { |
| 136 | + return { |
| 137 | + attrs: { type: "application/ld+json" }, |
| 138 | + content: JSON.stringify(data).replaceAll("<", String.raw`<`), |
| 139 | + tag: "script", |
| 140 | + }; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Returns the elements that a page adds to its `<head>`: the Open Graph |
| 145 | + * image of its package, or of the landing page, and its structured data. |
| 146 | + * The 404 page gets the image only. |
| 147 | + */ |
| 148 | +function seoHead(page: PageInfo, isNotFound: boolean): HeadEntry[] { |
| 149 | + const image = openGraphImageTags(page.siteUrl, page.folder); |
| 150 | + return isNotFound ? image : [...image, jsonLd(structuredData(page))]; |
| 151 | +} |
| 152 | + |
| 153 | +export { seoHead }; |
| 154 | +export type { PageInfo }; |
0 commit comments