|
4 | 4 | // title, image or template gives it a new URL and link previews refresh. Cards are English at |
5 | 5 | // every locale. |
6 | 6 | // |
| 7 | +// OG Studio (a local design tool) can replace any card. It writes finished files and a manifest to |
| 8 | +// src/asset/og-studio/: for each page an English PNG, an optional MP4 for og:video and an optional |
| 9 | +// PNG for each locale. Pages in that manifest use its files; every other page renders here. |
| 10 | +// |
7 | 11 | // This file is the cards' source of truth. The layout is David's template frame in Paper (the |
8 | 12 | // "OG images" file, frame "/ — dark"): a grid of #2e2e2e lines at x 72, 900 and 1128 and y 72, |
9 | 13 | // 557 and 629, the logo icon in the top-left cell, the title and a description of at most two |
@@ -19,6 +23,7 @@ import { decompress } from "wawoff2" |
19 | 23 |
|
20 | 24 | const root = path.resolve(import.meta.dir, "..") |
21 | 25 | const out = path.join(root, "public/og") |
| 26 | +const STUDIO = "src/asset/og-studio" |
22 | 27 |
|
23 | 28 | const W = 1200 |
24 | 29 | const H = 630 |
@@ -202,8 +207,27 @@ const fonts = [ |
202 | 207 | ] |
203 | 208 |
|
204 | 209 | await mkdir(out, { recursive: true }) |
205 | | -const manifest: Record<string, { src: string; alt: string }> = {} |
206 | | -for (const card of cards) { |
| 210 | +const manifest: Record<string, OgEntry> = {} |
| 211 | +const studio = await readStudio() |
| 212 | +for (const [page, entry] of Object.entries(studio?.cards ?? {})) { |
| 213 | + const files = [entry.image, entry.video, ...Object.values(entry.locales ?? {}).map((l) => l.image)].filter( |
| 214 | + (file): file is string => Boolean(file), |
| 215 | + ) |
| 216 | + await Promise.all(files.map((file) => Bun.write(path.join(out, file), Bun.file(path.join(root, STUDIO, file))))) |
| 217 | + manifest[page] = { |
| 218 | + src: `/og/${entry.image}`, |
| 219 | + alt: entry.alt, |
| 220 | + ...(entry.video ? { video: `/og/${entry.video}` } : {}), |
| 221 | + ...(entry.locales |
| 222 | + ? { |
| 223 | + locales: Object.fromEntries( |
| 224 | + Object.entries(entry.locales).map(([locale, l]) => [locale, { src: `/og/${l.image}`, alt: l.alt }]), |
| 225 | + ), |
| 226 | + } |
| 227 | + : {}), |
| 228 | + } |
| 229 | +} |
| 230 | +for (const card of cards.filter((card) => !manifest[card.path])) { |
207 | 231 | await checkDescription(card) |
208 | 232 | const image = card.shot === "orb" ? await orb() : await project(card.shot) |
209 | 233 | const svg = await satori(plate(card, image), { width: W, height: H, fonts }) |
@@ -216,12 +240,30 @@ for (const card of cards) { |
216 | 240 | alt: `OpenCode link card: “${card.title}” and “${card.description}” beside ${card.shows}.`, |
217 | 241 | } |
218 | 242 | } |
219 | | -const keep = new Set(Object.values(manifest).map((card) => path.basename(card.src))) |
| 243 | +const keep = new Set( |
| 244 | + Object.values(manifest).flatMap((card) => |
| 245 | + [card.src, card.video, ...Object.values(card.locales ?? {}).map((l) => l.src)] |
| 246 | + .filter((src): src is string => Boolean(src)) |
| 247 | + .map((src) => path.basename(src)), |
| 248 | + ), |
| 249 | +) |
220 | 250 | await Promise.all( |
221 | 251 | (await readdir(out)).filter((file) => !keep.has(file)).map((file) => rm(path.join(out, file), { force: true })), |
222 | 252 | ) |
223 | 253 | await Bun.write(path.join(root, "src/lib/og-images.json"), JSON.stringify(manifest, null, 2) + "\n") |
224 | | -console.log(`og: ${cards.length} cards in public/og`) |
| 254 | +console.log(`og: ${Object.keys(manifest).length} cards in public/og (${Object.keys(studio?.cards ?? {}).length} from OG Studio)`) |
| 255 | + |
| 256 | +type OgEntry = { src: string; alt: string; video?: string; locales?: Record<string, { src: string; alt: string }> } |
| 257 | + |
| 258 | +type Studio = { |
| 259 | + cards: Record<string, { image: string; alt: string; video?: string; locales?: Record<string, { image: string; alt: string }> }> |
| 260 | +} |
| 261 | + |
| 262 | +async function readStudio() { |
| 263 | + const file = Bun.file(path.join(root, STUDIO, "manifest.json")) |
| 264 | + if (!(await file.exists())) return undefined |
| 265 | + return (await file.json()) as Studio |
| 266 | +} |
225 | 267 |
|
226 | 268 | function plate(card: Card, image: string) { |
227 | 269 | const size = titleSize(card.title) |
|