// Share images for links on social sites and chat apps: every cover in public/covers becomes a // 1200 x 630 JPEG at the same path in public/og (e.g. covers/claude/x.webp -> og/claude/x.jpg), // cropped to the middle of the 5:3 cover. `npm run covers` runs this after rendering; run it on its own // with `node scripts/covers/og.mjs` to redo the share images from the covers already in public/covers. import fs from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import sharp from "sharp"; const COVERS_DIR = path.join(process.cwd(), "public", "covers"); const OG_DIR = path.join(process.cwd(), "public", "og"); async function coverFiles(dir) { const entries = await fs.readdir(dir, { withFileTypes: true }); const nested = await Promise.all( entries.map((e) => (e.isDirectory() ? coverFiles(path.join(dir, e.name)) : e.name.endsWith(".webp") ? [path.join(dir, e.name)] : [])) ); return nested.flat(); } export async function writeOgImages() { for (const cover of await coverFiles(COVERS_DIR)) { const out = path.join(OG_DIR, path.relative(COVERS_DIR, cover)).replace(/\.webp$/, ".jpg"); await fs.mkdir(path.dirname(out), { recursive: true }); await sharp(cover).resize(1200, 630, { fit: "cover", position: "centre" }).jpeg({ quality: 84, mozjpeg: true }).toFile(out); console.log(`og: wrote ${path.relative(process.cwd(), out)}`); } } if (process.argv[1] === fileURLToPath(import.meta.url)) { writeOgImages().catch((e) => { console.error("og: failed:", e); process.exitCode = 1; }); }