// Generates public/og-image.png (1200x630) and its alt text from the site
// metadata and the pixel avatar. Run with `pnpm og-image`.
//
// Needs two system tools that the rest of the project does not:
// woff2_decompress (font/woff2) - turns the self-hosted woff2 files into
// TTF, because librsvg cannot read woff2
// rsvg-convert (librsvg) - rasterizes the SVG card
// The generated PNG is committed, so neither tool is needed to build or deploy.
import { execFileSync } from 'node:child_process'
import { copyFileSync, mkdirSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { SITE } from '../site.config.mjs'
import { AVATAR_PALETTE, AVATAR_SPRITE } from '../app/utils/avatar.js'
const WIDTH = 1200
const HEIGHT = 630
const ROOT = join(import.meta.dirname, '..')
const OUT_DIR = join(ROOT, 'public')
const FONT_DIR = join(ROOT, 'node_modules', '.cache', 'og-image-fonts')
// Brand fonts, normalised name -> file in public/fonts.
const FONTS = [
['Inter', 'inter-var.woff2'],
['JetBrains Mono', 'jetbrains-mono-var.woff2'],
// The variable Newsreader file carries its optical-size name.
['Newsreader 16pt', 'newsreader-var.woff2'],
]
// --- fonts -----------------------------------------------------------------
const prepareFonts = () => {
mkdirSync(FONT_DIR, { recursive: true })
for (const [, file] of FONTS) {
copyFileSync(join(ROOT, 'public', 'fonts', file), join(FONT_DIR, file))
execFileSync('woff2_decompress', [join(FONT_DIR, file)], { stdio: 'ignore' })
}
const conf = join(FONT_DIR, 'fonts.conf')
writeFileSync(conf, `
${FONT_DIR}
${join(FONT_DIR, 'cache')}
`)
return conf
}
// --- card ------------------------------------------------------------------
const avatar = () => {
const scale = 8
const x = 88
const y = (HEIGHT - 32 * scale) / 2
const cells = []
for (let row = 0; row < 32; row++) {
for (let col = 0; col < 32; col++) {
const color = AVATAR_PALETTE[AVATAR_SPRITE[row][col]]
if (!color) continue
cells.push(
``,
)
}
}
return [
// Chip, the same surface the avatar sits on in the header.
``,
``,
``,
...cells,
'',
].join('\n')
}
const card = () => {
const textX = 400
const textRight = WIDTH - 88
const mono = `font-family="'JetBrains Mono', monospace"`
const sans = `font-family="'Inter', sans-serif"`
const serif = `font-family="'Newsreader 16pt', serif"`
return `
`
}
// --- write -----------------------------------------------------------------
const fontsConf = prepareFonts()
const svgFile = join(FONT_DIR, 'og-image.svg')
const pngFile = join(OUT_DIR, 'og-image.png')
writeFileSync(svgFile, card())
execFileSync(
'rsvg-convert',
['-w', String(WIDTH), '-h', String(HEIGHT), svgFile, '-o', pngFile],
{ env: { ...process.env, FONTCONFIG_FILE: fontsConf }, stdio: 'inherit' },
)
writeFileSync(
join(OUT_DIR, 'og-image.alt.txt'),
`${SITE.name} — ${SITE.role}. Pixel portrait next to the CV title, with the line "Every chapter runs as a real Kubernetes workload — knock it down and watch it recover."\n`,
)
console.log(`wrote ${pngFile} and ${join(OUT_DIR, 'og-image.alt.txt')}`)