110 lines
3.7 KiB
TypeScript
110 lines
3.7 KiB
TypeScript
// 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<Response> {
|
|||
|
|
const svg = wheelSvg(chart, timeKnown, WHEEL);
|
|||
|
|
return new ImageResponse(
|
|||
|
|
(
|
|||
|
|
<div style={{ display: "flex", width: W, height: H, background: "#0d1220", color: "#ece6d8", fontFamily: "Inter" }}>
|
|||
|
|
<div style={{ display: "flex", width: WHEEL + 40, height: H, alignItems: "center", justifyContent: "center" }}>
|
|||
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|||
|
|
<img
|
|||
|
|
src={`data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}`}
|
|||
|
|
width={WHEEL}
|
|||
|
|
height={WHEEL}
|
|||
|
|
alt=""
|
|||
|
|
/>
|
|||
|
|
</div>
|
|||
|
|
<div style={{ display: "flex", flexDirection: "column", flex: 1, padding: "56px 56px 44px 8px", gap: 18 }}>
|
|||
|
|
<div style={{ display: "flex", fontSize: 17, fontWeight: 600, letterSpacing: 2.5, color: "#d9b46a" }}>
|
|||
|
|
{printable(eyebrow)}
|
|||
|
|
</div>
|
|||
|
|
<div style={{ display: "flex", fontFamily: "Cormorant Garamond", fontSize: 56, lineHeight: 1.05 }}>
|
|||
|
|
{printable(title)}
|
|||
|
|
</div>
|
|||
|
|
{text && (
|
|||
|
|
<div style={{ display: "flex", fontSize: 23, lineHeight: 1.45, color: "#c9cfdc" }}>{printable(text)}</div>
|
|||
|
|
)}
|
|||
|
|
<div style={{ display: "flex", flexWrap: "wrap", marginTop: "auto", gap: "8px 22px", fontSize: 21, color: "#c9cfdc" }}>
|
|||
|
|
{lines.map((l) => (
|
|||
|
|
<div key={l.label} style={{ display: "flex", alignItems: "center", gap: 8 }}>
|
|||
|
|
<span style={{ fontFamily: "DejaVu Sans", color: "#d9b46a" }}>{l.glyph.replace(/︎/g, "")}</span>
|
|||
|
|
<span>{printable(l.label)}</span>
|
|||
|
|
</div>
|
|||
|
|
))}
|
|||
|
|
</div>
|
|||
|
|
<div style={{ display: "flex", fontFamily: "Cormorant Garamond", fontSize: 30, color: "#ece6d8" }}>
|
|||
|
|
sors<span style={{ color: "#d9b46a" }}>fonalak</span>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
),
|
|||
|
|
{ width: W, height: H, fonts: await loadFonts(), headers: { "Cache-Control": cacheControl } },
|
|||
|
|
);
|
|||
|
|
}
|