astro-og-canvas
This package provides utlities to generate OpenGraph images for the pages on your Astro site.
Installation
npm i astro-og-canvas
Using pnpm
? pnpm
users will also need to install canvaskit-wasm
as a direct dependency:
pnpm i canvaskit-wasm
Version compatibility
Usage
Creating an OpenGraph image endpoint
-
Create a new file in your src/pages/
directory. For example, src/pages/open-graph/[...route].ts
.
-
Use the OGImageRoute
helper to create getStaticPaths
and GET
functions for you:
import { OGImageRoute } from 'astro-og-canvas';
export const { getStaticPaths, GET } = OGImageRoute({
param: 'route',
pages: await import.meta.glob('/src/pages/**/*.md', { eager: true }),
getImageOptions: (path, page) => ({
title: page.frontmatter.title,
description: page.frontmatter.description,
logo: {
path: './src/astro-docs-logo.png',
},
}),
});
Image Options
Your getImageOptions
callback should return an object configuring the image to render. Almost all options are optional.
export interface OGImageOptions {
title: string;
description?: string;
dir?: 'rtl' | 'ltr';
logo?: {
path: string;
size?: [width?: number, height?: number];
};
bgGradient?: RGBColor[];
border?: {
color?: RGBColor;
width?: number;
side?: LogicalSide;
};
padding?: number;
font?: {
title?: FontConfig;
description?: FontConfig;
};
fonts?: string[];
}
FontConfig
export interface FontConfig {
color?: RGBColor;
size?: number;
weight?: Exclude<keyof CanvasKit['FontWeight'], 'values'>;
lineHeight?: number;
families?: string[];
}
## License
MIT