0.3.0 • Published 11 months ago
satori-astro v0.3.0
satori-astro
This is an helper function for Astro to help you generate OpenGraph images using satori and sharp.
Usage
Installation
Install the required dependencies:
pnpm add satori-astro sharpnpm install satori-astro sharpyarn add satori-astro sharpsatoriAstroOG
satoriAstroOG can be used in 3 ways, depending on the level of abstraction you need. We recommend going with toResponse by default. For example
// src/pages/og.png.ts
import type { APIRoute } from "astro";
import { satoriAstroOG } from "satori-astro";
import { html } from "satori-html";
export const GET: APIRoute = async () => {
const fontFile = await fetch(
"https://og-playground.vercel.app/inter-latin-ext-700-normal.woff",
);
const fontData: ArrayBuffer = await fontFile.arrayBuffer();
return await satoriAstroOG({
template: html`<div style="display: flex; justify-items: center; align-items: center; background-color: red; font-family: Inter; height: 100%;">
<h1 style="color: blue;">Test toResponse</h1>
</div>`,
width: 1920,
height: 1080,
}).toResponse({
satori: {
fonts: [
{
name: "Inter Latin",
data: fontData,
style: "normal",
},
],
},
});
};toSvg: returns a svg asstring. It requires font data to be passed in. Any other satori option is optionaltoImage: returns an image asBuffer. Built on top oftoSvg, it requires ìts options as thesatoriproperty. It also allows passing custom resvg options as theresvgpropertytoResponse: returns aResponse. Built on top oftoImage, it accepts the same options as well asresponse, that can be used to aspects of the response (like headers).
Contributing
This package is structured as a monorepo:
playgroundcontains code for testing the packagepackagecontains the actual package
Install dependencies using pnpm:
pnpm i --frozen-lockfileStart the playground:
pnpm playground:devYou can now edit files in package. Please note that making changes to those files may require restarting the playground dev server.
Licensing
MIT Licensed. Made with ❤️ by Florian Lefebvre.