// Link preview images (og:image): 1200×630 PNG with a chart wheel on the left // and text on the right, in the site's look. Used by the shared charts and // the home page. import "server-only"; import { readFile } from "node:fs/promises"; import { join } from "node:path"; import { ImageResponse } from "next/og"; import type { ChartResult } from "./types"; import { wheelSvg } from "./wheelSvg"; const W = 1200; const H = 630; const WHEEL = 590; const FONTS = join(process.cwd(), "src/assets/og"); let fonts: Promise<{ name: string; data: Buffer; weight: 400 | 600; style: "normal" }[]> | null = null; function loadFonts() { fonts ??= Promise.all( ( [ ["Cormorant Garamond", "CormorantGaramond-SemiBold.ttf", 600], ["Inter", "Inter-Regular.ttf", 400], ["Inter", "Inter-SemiBold.ttf", 600], ["DejaVu Sans", "DejaVuSans.ttf", 400], ] as const ).map(async ([name, file, weight]) => ({ name, data: await readFile(join(FONTS, file)), weight, style: "normal" as const, })), ); return fonts; } /** * Letters, digits, punctuation and spaces only: a character missing from the * fonts would make the renderer download a font from the internet. */ export const printable = (s: string) => s.replace(/[^\p{L}\p{N}\p{P}\p{Zs}+=<>°]/gu, ""); export interface CardLine { /** A zodiac or planet glyph (DejaVu Sans has them all). */ glyph: string; label: string; } /** The preview image as a response with the given cache header. */ export async function chartCard({ chart, timeKnown, eyebrow, title, text, lines, cacheControl, }: { chart: ChartResult; timeKnown: boolean; eyebrow: string; title: string; text: string; lines: CardLine[]; cacheControl: string; }): Promise { const svg = wheelSvg(chart, timeKnown, WHEEL); return new ImageResponse( (
{/* eslint-disable-next-line @next/next/no-img-element */}
{printable(eyebrow)}
{printable(title)}
{text && (
{printable(text)}
)}
{lines.map((l) => (
{l.glyph.replace(/︎/g, "")} {printable(l.label)}
))}
sorsfonalak
), { width: W, height: H, fonts: await loadFonts(), headers: { "Cache-Control": cacheControl } }, ); }