#!/usr/bin/env node // Adds the approved headline locally so spelling does not depend on the image model. // Usage: node scripts/add-headline.mjs --input edited.png --headline "STOP SCROLLING" --output final.png import sharp from 'sharp' import { readFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' const root = join(dirname(fileURLToPath(import.meta.url)), '..') const options = {} const args = process.argv.slice(2) for (let index = 0; index < args.length; index += 1) { const key = args[index] if (!key.startsWith('--')) continue options[key.slice(2)] = args[index + 1] index += 1 } if (!options.input || !options.headline || !options.output) { console.error('Usage: node scripts/add-headline.mjs --input edited.png --headline "TWO TO SIX WORDS" --output final.png') process.exit(1) } const words = options.headline.trim().toUpperCase().split(/\s+/).filter(Boolean) if (words.length < 2 || words.length > 6) { console.error('The headline must have two to six words.') process.exit(1) } const style = JSON.parse(readFileSync(join(root, 'assets/headline-style.json'), 'utf8')) const lines = [] let line = '' for (const word of words) { const next = line ? `${line} ${word}` : word if (next.length > 13 && line && lines.length < 2) { lines.push(line) line = word } else { line = next } } if (line) lines.push(line) const escapeXml = (value) => value.replace(/[<>&'"]/g, (char) => ({ '<': '<', '>': '>', '&': '&', "'": ''', '"': '"' })[char]) const totalHeight = lines.length * style.line_height const startY = Math.round((style.height - totalHeight) / 2 + style.font_size * .8) const text = lines.map((value, index) => `${escapeXml(value)}` ).join('') const overlay = `${text}` await sharp(options.input) .resize(style.width, style.height, { fit: 'cover' }) .composite([{ input: Buffer.from(overlay), top: 0, left: 0 }]) .png() .toFile(options.output) console.log(options.output)