Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

feat(docs): dynamic open graph images for `/integrations` (#1684)

Colton Padden007cd0fbd9a7
diff --git a/apps/docs/app/[lang]/integrations/[slug]/opengraph-image.tsx b/apps/docs/app/[lang]/integrations/[slug]/opengraph-image.tsx
new file mode 100644
index 000000000..40d868f75
--- /dev/null
+++ b/apps/docs/app/[lang]/integrations/[slug]/opengraph-image.tsx
@@ -0,0 +1,26 @@
+import { notFound } from "next/navigation";
+import { getIntegration } from "@/lib/integrations/data";
+import { createIntegrationOgImage } from "@/lib/integrations/og-image";
+
+export const size = {
+ width: 1200,
+ height: 628,
+};
+export const contentType = "image/png";
+
+const IntegrationOpenGraphImage = async ({
+ params,
+}: {
+ params: Promise<{ lang: string; slug: string }>;
+}) => {
+ const { slug } = await params;
+ const integration = getIntegration(slug);
+
+ if (!integration) {
+ notFound();
+ }
+
+ return createIntegrationOgImage(integration);
+};
+
+export default IntegrationOpenGraphImage;
diff --git a/apps/docs/app/[lang]/integrations/[slug]/page.tsx b/apps/docs/app/[lang]/integrations/[slug]/page.tsx
index 45472f819..b562baa4e 100644
--- a/apps/docs/app/[lang]/integrations/[slug]/page.tsx
+++ b/apps/docs/app/[lang]/integrations/[slug]/page.tsx
@@ -5,7 +5,6 @@ import { notFound } from "next/navigation";
import { Suspense } from "react";
import { canonicalAlternates, integrationPath } from "@/lib/geistdocs/canonical";
import { pageTitleMetadata } from "@/lib/geistdocs/metadata-title";
-import { staticOgImage } from "@/lib/geistdocs/og";
import { buildConnectionInstall, buildConnectionSetup } from "@/lib/integrations/connection-setup";
import {
getIntegration,
@@ -47,11 +46,10 @@ export const generateMetadata = async ({
...titleMetadata,
description: integration.tagline,
alternates: canonicalAlternates(integrationPath(integration.slug)),
- openGraph: { ...titleMetadata.openGraph, images: [staticOgImage] },
+ openGraph: titleMetadata.openGraph,
twitter: {
...titleMetadata.twitter,
card: "summary_large_image",
- images: [staticOgImage],
},
};
};
diff --git a/apps/docs/app/[lang]/integrations/[slug]/twitter-image.tsx b/apps/docs/app/[lang]/integrations/[slug]/twitter-image.tsx
new file mode 100644
index 000000000..f7e5315c9
--- /dev/null
+++ b/apps/docs/app/[lang]/integrations/[slug]/twitter-image.tsx
@@ -0,0 +1,26 @@
+import { notFound } from "next/navigation";
+import { getIntegration } from "@/lib/integrations/data";
+import { createIntegrationOgImage } from "@/lib/integrations/og-image";
+
+export const size = {
+ width: 1200,
+ height: 628,
+};
+export const contentType = "image/png";
+
+const IntegrationTwitterImage = async ({
+ params,
+}: {
+ params: Promise<{ lang: string; slug: string }>;
+}) => {
+ const { slug } = await params;
+ const integration = getIntegration(slug);
+
+ if (!integration) {
+ notFound();
+ }
+
+ return createIntegrationOgImage(integration);
+};
+
+export default IntegrationTwitterImage;
diff --git a/apps/docs/app/[lang]/integrations/opengraph-image.tsx b/apps/docs/app/[lang]/integrations/opengraph-image.tsx
new file mode 100644
index 000000000..bff3b23af
--- /dev/null
+++ b/apps/docs/app/[lang]/integrations/opengraph-image.tsx
@@ -0,0 +1,11 @@
+import { createIntegrationsDirectoryOgImage } from "@/lib/integrations/directory-og-image";
+
+export const size = {
+ width: 1200,
+ height: 628,
+};
+export const contentType = "image/png";
+
+const IntegrationsOpenGraphImage = () => createIntegrationsDirectoryOgImage();
+
+export default IntegrationsOpenGraphImage;
diff --git a/apps/docs/app/[lang]/integrations/page.tsx b/apps/docs/app/[lang]/integrations/page.tsx
index 50f0894bd..38438eacd 100644
--- a/apps/docs/app/[lang]/integrations/page.tsx
+++ b/apps/docs/app/[lang]/integrations/page.tsx
@@ -1,7 +1,6 @@
import type { Metadata } from "next";
import { canonicalAlternates, canonicalRoutes } from "@/lib/geistdocs/canonical";
import { pageTitleMetadata } from "@/lib/geistdocs/metadata-title";
-import { staticOgImage } from "@/lib/geistdocs/og";
import { integrations } from "@/lib/integrations/data";
import { translations } from "@/geistdocs";
import { Gallery, type GalleryFilter } from "./components/gallery";
@@ -15,11 +14,10 @@ export const metadata: Metadata = {
...titleMetadata,
description,
alternates: canonicalAlternates(canonicalRoutes.integrations),
- openGraph: { ...titleMetadata.openGraph, images: [staticOgImage] },
+ openGraph: titleMetadata.openGraph,
twitter: {
...titleMetadata.twitter,
card: "summary_large_image",
- images: [staticOgImage],
},
};
diff --git a/apps/docs/app/[lang]/integrations/twitter-image.tsx b/apps/docs/app/[lang]/integrations/twitter-image.tsx
new file mode 100644
index 000000000..480e6d7c4
--- /dev/null
+++ b/apps/docs/app/[lang]/integrations/twitter-image.tsx
@@ -0,0 +1,11 @@
+import { createIntegrationsDirectoryOgImage } from "@/lib/integrations/directory-og-image";
+
+export const size = {
+ width: 1200,
+ height: 628,
+};
+export const contentType = "image/png";
+
+const IntegrationsTwitterImage = () => createIntegrationsDirectoryOgImage();
+
+export default IntegrationsTwitterImage;
diff --git a/apps/docs/lib/integrations/directory-og-image.tsx b/apps/docs/lib/integrations/directory-og-image.tsx
new file mode 100644
index 000000000..c042972bb
--- /dev/null
+++ b/apps/docs/lib/integrations/directory-og-image.tsx
@@ -0,0 +1,118 @@
+import { readFile } from "node:fs/promises";
+import { join } from "node:path";
+import { ImageResponse } from "next/og";
+
+const integrationsDirectoryOgImageSize = {
+ width: 1200,
+ height: 628,
+};
+
+const fontDirectory = join(process.cwd(), "app/[lang]/og/[...slug]");
+const fonts = Promise.all([
+ readFile(join(fontDirectory, "geist-sans-regular.ttf")),
+ readFile(join(fontDirectory, "geist-sans-semibold.ttf")),
+]);
+
+export const createIntegrationsDirectoryOgImage = async (): Promise<ImageResponse> => {
+ const [regularFont, semiboldFont] = await fonts;
+
+ return new ImageResponse(
+ <div
+ style={{
+ background: "black",
+ display: "flex",
+ fontFamily: "Geist",
+ height: "100%",
+ position: "relative",
+ width: "100%",
+ }}
+ >
+ <svg
+ fill="none"
+ height="628"
+ style={{ inset: 0, position: "absolute" }}
+ viewBox="0 0 1200 628"
+ width="1200"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ d="M1135.17 213.99H995.554L898.062 334.984H867.16L983.038 190.984H1135.17V213.99Z"
+ fill="white"
+ />
+ <path d="M1135.17 311.919V334.925H1010.54V311.919H1135.17Z" fill="white" />
+ <path d="M800.63 334.921H676V311.915H800.63V334.921Z" fill="white" />
+ <path d="M781.034 273.983H676V250.981H781.034V273.983Z" fill="white" />
+ <path d="M1135.17 273.983H1030.13V250.981H1135.17V273.983Z" fill="white" />
+ <path d="M881.178 213.99H676V190.984H881.178V213.99Z" fill="white" />
+ <rect
+ fill="url(#eve-logo-fade)"
+ height="264.03"
+ transform="rotate(-45 546 304.914)"
+ width="410"
+ x="546"
+ y="304.914"
+ />
+ <path d="M92 47L124 103H60L92 47Z" fill="white" />
+ <defs>
+ <linearGradient
+ gradientUnits="userSpaceOnUse"
+ id="eve-logo-fade"
+ x1="751"
+ x2="751"
+ y1="304.914"
+ y2="568.943"
+ >
+ <stop offset="0.144231" stopColor="black" />
+ <stop offset="1" stopColor="black" stopOpacity="0" />
+ </linearGradient>
+ </defs>
+ </svg>
+ <div
+ style={{
+ color: "#A0A0A0",
+ display: "flex",
+ fontSize: 26.4,
+ fontWeight: 500,
+ left: 60,
+ letterSpacing: "-0.03em",
+ lineHeight: 1,
+ position: "absolute",
+ top: 192,
+ }}
+ >
+ eve
+ </div>
+ <div
+ style={{
+ color: "white",
+ display: "flex",
+ fontSize: 80,
+ fontWeight: 400,
+ left: 60,
+ letterSpacing: "-0.06em",
+ lineHeight: 1,
+ position: "absolute",
+ top: 242,
+ whiteSpace: "nowrap",
+ }}
+ >
+ Integrations
+ </div>
+ </div>,
+ {
+ ...integrationsDirectoryOgImageSize,
+ fonts: [
+ {
+ name: "Geist",
+ data: regularFont,
+ weight: 400,
+ },
+ {
+ name: "Geist",
+ data: semiboldFont,
+ weight: 500,
+ },
+ ],
+ },
+ );
+};
diff --git a/apps/docs/lib/integrations/og-image.tsx b/apps/docs/lib/integrations/og-image.tsx
new file mode 100644
index 000000000..a2ecfa624
--- /dev/null
+++ b/apps/docs/lib/integrations/og-image.tsx
@@ -0,0 +1,305 @@
+import { LogoEve } from "@vercel/geistdocs/assets/logos/logo-eve";
+import { ImageResponse } from "next/og";
+import { PNG } from "pngjs";
+import { Children, cloneElement, isValidElement, type ReactElement, type ReactNode } from "react";
+import type { Integration } from "./data";
+import { logos } from "./logos";
+
+const integrationOgImageSize = {
+ width: 1200,
+ height: 628,
+};
+
+type LogoElementProps = Record<string, unknown> & {
+ children?: ReactNode;
+ fill?: string;
+ height?: number;
+ preserveAspectRatio?: string;
+ stroke?: string;
+ viewBox?: string;
+ width?: number;
+};
+
+const resolveLogo = (node: ReactNode): ReactNode => {
+ if (!isValidElement(node)) return node;
+
+ const element = node as ReactElement<LogoElementProps>;
+ if (element.type === "title" || element.type === "desc") return null;
+ if (typeof element.type === "function") {
+ const component = element.type as (props: LogoElementProps) => ReactNode;
+ return resolveLogo(component(element.props));
+ }
+ if (typeof element.type === "object" && "render" in element.type) {
+ const component = element.type as { render: (props: LogoElementProps) => ReactNode };
+ return resolveLogo(component.render(element.props));
+ }
+
+ return cloneElement(element, element.props, Children.map(element.props.children, resolveLogo));
+};
+
+const fitLogo = (node: ReactNode, maxWidth: number, maxHeight: number): ReactNode => {
+ if (!isValidElement(node)) return node;
+
+ const element = node as ReactElement<LogoElementProps>;
+ const viewBox = element.props.viewBox
+ ?.trim()
+ .split(/[\s,]+/)
+ .map(Number);
+ if (!viewBox || viewBox.length !== 4 || viewBox.some((value) => !Number.isFinite(value))) {
+ return cloneElement(element, { ...element.props, height: maxHeight, width: maxWidth });
+ }
+
+ const [, , viewBoxWidth, viewBoxHeight] = viewBox;
+ if (viewBoxWidth <= 0 || viewBoxHeight <= 0) return element;
+
+ const aspectRatio = viewBoxWidth / viewBoxHeight;
+ const width = Math.min(maxWidth, maxHeight * aspectRatio);
+ const height = width / aspectRatio;
+ return cloneElement(element, {
+ ...element.props,
+ height,
+ preserveAspectRatio: "xMidYMid meet",
+ width,
+ });
+};
+
+interface RasterizedLogo {
+ height: number;
+ inkCoverage: number;
+ src: string;
+ width: number;
+}
+
+interface SizedLogo {
+ height: number;
+ src: string;
+ width: number;
+}
+
+const pixelInk = (image: PNG, offset: number): number => {
+ const red = image.data[offset];
+ const green = image.data[offset + 1];
+ const blue = image.data[offset + 2];
+ const alpha = image.data[offset + 3];
+ const luminance = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
+ return (alpha * luminance) / 255;
+};
+
+const cropLogo = (image: PNG): PNG => {
+ let minX = image.width;
+ let minY = image.height;
+ let maxX = -1;
+ let maxY = -1;
+
+ for (let y = 0; y < image.height; y += 1) {
+ for (let x = 0; x < image.width; x += 1) {
+ const offset = (y * image.width + x) * 4;
+ if (pixelInk(image, offset) <= 8) continue;
+ minX = Math.min(minX, x);
+ minY = Math.min(minY, y);
+ maxX = Math.max(maxX, x);
+ maxY = Math.max(maxY, y);
+ }
+ }
+
+ if (maxX < minX || maxY < minY) return image;
+
+ const width = maxX - minX + 1;
+ const height = maxY - minY + 1;
+ const cropped = new PNG({ height, width });
+ PNG.bitblt(image, cropped, minX, minY, width, height, 0, 0);
+ return cropped;
+};
+
+const rasterizeLogo = async (logo: ReactNode): Promise<RasterizedLogo> => {
+ const response = new ImageResponse(
+ <div
+ style={{
+ alignItems: "center",
+ color: "white",
+ display: "flex",
+ height: "100%",
+ justifyContent: "center",
+ width: "100%",
+ }}
+ >
+ {fitLogo(logo, 480, 480)}
+ </div>,
+ { height: 512, width: 512 },
+ );
+ const image = PNG.sync.read(Buffer.from(await response.arrayBuffer()));
+ const cropped = cropLogo(image);
+ let ink = 0;
+ for (let offset = 0; offset < cropped.data.length; offset += 4) {
+ ink += pixelInk(cropped, offset) / 255;
+ }
+
+ return {
+ height: cropped.height,
+ inkCoverage: ink / (cropped.width * cropped.height),
+ src: `data:image/png;base64,${PNG.sync.write(cropped).toString("base64")}`,
+ width: cropped.width,
+ };
+};
+
+const collectLogoColors = (node: ReactNode, colors = new Set<string>()): Set<string> => {
+ if (!isValidElement(node)) return colors;
+
+ const { children, fill, stroke } = (node as ReactElement<LogoElementProps>).props;
+ for (const color of [fill, stroke]) {
+ if (color && color !== "none" && color !== "currentColor") colors.add(color.toLowerCase());
+ }
+ Children.forEach(children, (child) => collectLogoColors(child, colors));
+ return colors;
+};
+
+const grayscaleColor = (color: string): string => {
+ const normalized = color.toLowerCase();
+ if (normalized === "currentcolor") return "white";
+ if (normalized === "white") return "black";
+ if (normalized === "black") return "white";
+
+ const hex = normalized.slice(1);
+ const expanded = hex.length === 3 ? [...hex].map((digit) => digit.repeat(2)).join("") : hex;
+ if (!/^[\da-f]{6}$/.test(expanded)) return "white";
+
+ const red = Number.parseInt(expanded.slice(0, 2), 16);
+ const green = Number.parseInt(expanded.slice(2, 4), 16);
+ const blue = Number.parseInt(expanded.slice(4, 6), 16);
+ const gray = 255 - Math.round(0.2126 * red + 0.7152 * green + 0.0722 * blue);
+ return `rgb(${gray}, ${gray}, ${gray})`;
+};
+
+const recolorLogo = (node: ReactNode, preserveTones: boolean): ReactNode => {
+ if (!isValidElement(node)) return node;
+
+ const element = node as ReactElement<LogoElementProps>;
+ const props = element.props;
+ const recolor = (color: string | undefined): string | undefined => {
+ if (!color || color === "none") return color;
+ return preserveTones ? grayscaleColor(color) : "white";
+ };
+
+ return cloneElement(
+ element,
+ { ...props, fill: recolor(props.fill), stroke: recolor(props.stroke) },
+ Children.map(props.children, (child) => recolorLogo(child, preserveTones)),
+ );
+};
+
+const balanceLogoWeight = (logo: RasterizedLogo, referenceCoverage: number): SizedLogo => {
+ const aspectRatio = logo.width / logo.height;
+ const fittedWidth = Math.min(180, 132 * aspectRatio);
+ const fittedHeight = fittedWidth / aspectRatio;
+ if (logo.inkCoverage <= 0) {
+ return { height: fittedHeight, src: logo.src, width: fittedWidth };
+ }
+
+ // Dampened density normalization keeps solid marks from overpowering the
+ // wordmark without making compact logos feel disproportionately small.
+ const densityScale = Math.min(1, (referenceCoverage / logo.inkCoverage) ** 0.25);
+ return {
+ height: fittedHeight * densityScale,
+ src: logo.src,
+ width: fittedWidth * densityScale,
+ };
+};
+
+let eveInkCoverage: Promise<number> | undefined;
+
+const getEveInkCoverage = (): Promise<number> => {
+ eveInkCoverage ??= rasterizeLogo(resolveLogo(<LogoEve />)).then((logo) => logo.inkCoverage);
+ return eveInkCoverage;
+};
+
+const rasterizedLogos = new Map<string, Promise<RasterizedLogo>>();
+
+const getRasterizedLogo = (integration: Integration): Promise<RasterizedLogo> => {
+ const cached = rasterizedLogos.get(integration.logo);
+ if (cached) return cached;
+
+ const Logo = logos[integration.logo];
+ const resolvedLogo = resolveLogo(<Logo aria-hidden />);
+ const recoloredLogo = recolorLogo(resolvedLogo, collectLogoColors(resolvedLogo).size > 1);
+ const rasterized = rasterizeLogo(recoloredLogo);
+ rasterizedLogos.set(integration.logo, rasterized);
+ return rasterized;
+};
+
+export const createIntegrationOgImage = async (
+ integration: Integration,
+): Promise<ImageResponse> => {
+ const [rasterizedLogo, referenceCoverage] = await Promise.all([
+ getRasterizedLogo(integration),
+ getEveInkCoverage(),
+ ]);
+ const integrationLogo = balanceLogoWeight(rasterizedLogo, referenceCoverage);
+
+ return new ImageResponse(
+ <div
+ style={{
+ alignItems: "center",
+ background: "black",
+ display: "flex",
+ height: "100%",
+ justifyContent: "center",
+ padding: 60,
+ width: "100%",
+ }}
+ >
+ <div
+ style={{
+ alignItems: "center",
+ display: "flex",
+ height: "100%",
+ justifyContent: "center",
+ width: "100%",
+ }}
+ >
+ <div
+ style={{
+ alignItems: "center",
+ color: "white",
+ display: "flex",
+ height: 132,
+ justifyContent: "flex-end",
+ width: 240,
+ }}
+ >
+ <LogoEve height={70} />
+ </div>
+ <div
+ style={{
+ alignItems: "center",
+ color: "white",
+ display: "flex",
+ fontSize: 56,
+ fontWeight: 300,
+ height: 132,
+ justifyContent: "center",
+ margin: "0 46px",
+ }}
+ >
+ +
+ </div>
+ <div
+ style={{
+ alignItems: "center",
+ display: "flex",
+ height: 132,
+ justifyContent: "flex-start",
+ width: 240,
+ }}
+ >
+ <img
+ alt=""
+ height={integrationLogo.height}
+ src={integrationLogo.src}
+ width={integrationLogo.width}
+ />
+ </div>
+ </div>
+ </div>,
+ integrationOgImageSize,
+ );
+};
diff --git a/apps/docs/package.json b/apps/docs/package.json
index 7cc6ab7d9..cd9af1d95 100644
--- a/apps/docs/package.json
+++ b/apps/docs/package.json
@@ -38,6 +38,7 @@
"lucide-react": "1.16.0",
"next": "catalog:",
"phase": "catalog:",
+ "pngjs": "^7.0.0",
"react": "catalog:",
"react-dom": "catalog:",
"sonner": "2.0.7",
@@ -88,7 +89,6 @@
"chat": "4.34.0",
"chat-adapter-sendblue": "0.2.0",
"eve": "workspace:*",
- "pngjs": "^7.0.0",
"postcss": "8.5.15",
"shadcn": "4.16.0",
"tailwindcss": "4.3.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4e95497d1..b0eef5572 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -194,6 +194,9 @@ importers:
phase:
specifier: 'catalog:'
version: 0.0.1([email protected])
+ pngjs:
+ specifier: ^7.0.0
+ version: 7.0.0
react:
specifier: 'catalog:'
version: 19.2.6
@@ -339,9 +342,6 @@ importers:
eve:
specifier: workspace:*
version: link:../../packages/eve
- pngjs:
- specifier: ^7.0.0
- version: 7.0.0
postcss:
specifier: 8.5.15
version: 8.5.15