import { readFile } from 'node:fs/promises' import { join } from 'node:path' import { ImageResponse } from 'next/og' import * as z from 'zod' import { siteConfig } from '@/app/seo' const ogImageSchema = z.object({ title: z.string().default(''), description: z.string().default(''), theme: z.enum(['light', 'dark']).default('dark'), }) let sansFont: ArrayBuffer | null = null let jakartaFont: ArrayBuffer | null = null async function loadFonts() { if (!(sansFont && jakartaFont)) { const fontDir = join(process.cwd(), 'public', 'fonts') const [sans, jakarta] = await Promise.all([ readFile(join(fontDir, 'geist-regular.ttf')), readFile(join(fontDir, 'PlusJakartaSans-SemiBold.ttf')), ]) sansFont = sans.buffer.slice(sans.byteOffset, sans.byteOffset + sans.byteLength) jakartaFont = jakarta.buffer.slice(jakarta.byteOffset, jakarta.byteOffset + jakarta.byteLength) } return { sans: sansFont, jakarta: jakartaFont } } function PascalLogo({ color = '#fff', size = 100 }: { color?: string; size?: number }) { return ( ) } export async function GET(req: Request) { try { const url = new URL(req.url) const values = ogImageSchema.parse(Object.fromEntries(url.searchParams)) const heading = values.title.length > 140 ? `${values.title.substring(0, 140)}...` : values.title const { theme } = values const paint = theme === 'dark' ? '#fff' : '#000' const logoColor = theme === 'dark' ? 'rgba(255, 255, 255, 0.9)' : 'rgba(0, 0, 0, 0.9)' const fontSize = heading.length > 100 ? '70px' : '100px' const showLargeLogo = !(values.title || values.description) return new ImageResponse(
{showLargeLogo ? (
Pascal Editor
{siteConfig.description}
) : (
{heading}
{values.description}
)} {!showLargeLogo && (
editor.pascal.app
)}
, { width: 1200, height: 630, fonts: await loadFonts().then(({ sans, jakarta }) => [ { name: 'sans-regular', data: sans, style: 'normal' as const, weight: 400 as const, }, { name: 'jakarta-semibold', data: jakarta, style: 'normal' as const, weight: 600 as const, }, ]), }, ) } catch (error) { console.log('error', error) return new Response('Failed to generate image', { status: 500, }) } }