44// title, image or template gives it a new URL and link previews refresh. Cards are English at
55// every locale.
66//
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+ //
711// This file is the cards' source of truth. The layout is David's template frame in Paper (the
812// "OG images" file, frame "/ — dark"): a grid of #2e2e2e lines at x 72, 900 and 1128 and y 72,
913// 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"
1923
2024const root = path . resolve ( import . meta. dir , ".." )
2125const out = path . join ( root , "public/og" )
26+ const STUDIO = "src/asset/og-studio"
2227
2328const W = 1200
2429const H = 630
@@ -202,8 +207,27 @@ const fonts = [
202207]
203208
204209await 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 ] ) ) {
207231 await checkDescription ( card )
208232 const image = card . shot === "orb" ? await orb ( ) : await project ( card . shot )
209233 const svg = await satori ( plate ( card , image ) , { width : W , height : H , fonts } )
@@ -216,12 +240,30 @@ for (const card of cards) {
216240 alt : `OpenCode link card: “${ card . title } ” and “${ card . description } ” beside ${ card . shows } .` ,
217241 }
218242}
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+ )
220250await Promise . all (
221251 ( await readdir ( out ) ) . filter ( ( file ) => ! keep . has ( file ) ) . map ( ( file ) => rm ( path . join ( out , file ) , { force : true } ) ) ,
222252)
223253await 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+ }
225267
226268function plate ( card : Card , image : string ) {
227269 const size = titleSize ( card . title )
0 commit comments