|
1 | | -import { copyFileSync, existsSync, mkdirSync, statSync } from "node:fs"; |
2 | | -import { fileURLToPath } from "node:url"; |
3 | | -import { dirname, join } from "node:path"; |
| 1 | +// Renders docs/og-img.svg -> public/static/og-img.png at 1200x630 (the |
| 2 | +// Open Graph standard) so social link previews unfurl with the proper |
| 3 | +// dimensions and stay under WhatsApp's ~600KB unfurl ceiling. |
| 4 | +// |
| 5 | +// docs/readme-hero.png is the README screenshot (different aspect ratio, |
| 6 | +// version chip baked in). It is intentionally NOT used for og:image |
| 7 | +// anymore — they have separate purposes. |
| 8 | +import { readFileSync, writeFileSync, existsSync, mkdirSync, statSync } from 'node:fs'; |
| 9 | +import { fileURLToPath } from 'node:url'; |
| 10 | +import { dirname, join } from 'node:path'; |
| 11 | +import { Resvg } from '@resvg/resvg-js'; |
4 | 12 |
|
5 | 13 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
6 | | -const root = join(__dirname, ".."); |
7 | | -const src = join(root, "docs/readme-hero.png"); |
8 | | -const dst = join(root, "public/static/og-img.png"); |
| 14 | +const root = join(__dirname, '..'); |
| 15 | +const srcPath = join(root, 'docs/og-img.svg'); |
| 16 | +const outPath = join(root, 'public/static/og-img.png'); |
9 | 17 |
|
10 | | -if (!existsSync(src)) { |
11 | | - console.error(`sync-hero: source missing: ${src}`); |
| 18 | +if (!existsSync(srcPath)) { |
| 19 | + console.error(`sync-hero: source missing: ${srcPath}`); |
12 | 20 | process.exit(1); |
13 | 21 | } |
14 | 22 |
|
15 | | -const srcMtime = statSync(src).mtimeMs; |
16 | | -const dstMtime = existsSync(dst) ? statSync(dst).mtimeMs : 0; |
17 | | - |
18 | | -if (srcMtime > dstMtime) { |
19 | | - mkdirSync(dirname(dst), { recursive: true }); |
20 | | - copyFileSync(src, dst); |
21 | | - console.log("sync-hero: copied docs/readme-hero.png -> public/static/og-img.png"); |
22 | | -} else { |
23 | | - console.log("sync-hero: og-img already up to date"); |
| 23 | +const srcMtime = statSync(srcPath).mtimeMs; |
| 24 | +const outMtime = existsSync(outPath) ? statSync(outPath).mtimeMs : 0; |
| 25 | +if (outMtime >= srcMtime) { |
| 26 | + console.log('sync-hero: og-img already up to date'); |
| 27 | + process.exit(0); |
24 | 28 | } |
| 29 | + |
| 30 | +mkdirSync(dirname(outPath), { recursive: true }); |
| 31 | +const svg = readFileSync(srcPath); |
| 32 | +const png = new Resvg(svg, { |
| 33 | + fitTo: { mode: 'width', value: 1200 }, |
| 34 | + background: '#0d6efd', |
| 35 | +}).render().asPng(); |
| 36 | + |
| 37 | +writeFileSync(outPath, png); |
| 38 | +const kb = (png.length / 1024).toFixed(1); |
| 39 | +console.log(`sync-hero: rendered docs/og-img.svg -> public/static/og-img.png (${kb} KB)`); |
0 commit comments