Skip to content

Commit 30741ff

Browse files
molloyzak13Debiankydecker
authored
feat: add automatic Open Graph preview images for docs pages using astro-og-canvas (#637)
Co-authored-by: molloyzak13 <molloyzak13@users.noreply.github.com> Co-authored-by: Debian <x@zak.linux> Co-authored-by: Ky Decker <itsevadecker@gmail.com>
1 parent 2650d56 commit 30741ff

9 files changed

Lines changed: 108 additions & 1 deletion

File tree

docs/astro.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import starlightThemeFlexoki from "starlight-theme-flexoki";
77
// https://astro.build/config
88
export default defineConfig({
99
site: "https://docs.namesake.fyi",
10-
adapter: cloudflare(),
10+
adapter: cloudflare({ prerenderEnvironment: "node" }),
1111
session: {
1212
driver: {
1313
entrypoint: "unstorage/drivers/null",
@@ -82,6 +82,7 @@ export default defineConfig({
8282
},
8383
],
8484
customCss: ["./src/styles/theme.css"],
85+
routeMiddleware: "./src/routeData.ts",
8586
}),
8687
],
8788
devToolbar: {

docs/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"@mermaid-js/layout-elk": "^0.2.1",
1919
"astro": "^6.4.5",
2020
"astro-mermaid": "^2.0.2",
21+
"astro-og-canvas": "^0.11.1",
22+
"canvaskit-wasm": "^0.41.1",
2123
"mermaid": "^11.15.0",
2224
"sharp": "^0.34.5",
2325
"starlight-theme-flexoki": "^0.2.2",

docs/src/content/docs/getting-started.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: Getting Started
3+
description: Want to help contribute to Namesake? Here's how to start.
34
---
45

56
import { LinkButton, LinkCard } from '@astrojs/starlight/components';
77.3 KB
Binary file not shown.
72.6 KB
Binary file not shown.

docs/src/pages/og/[...slug].ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { getCollection } from "astro:content";
2+
import { OGImageRoute } from "astro-og-canvas";
3+
4+
// Implementation based on: https://hideoo.dev/notes/starlight-og-images
5+
const entries = await getCollection("docs");
6+
7+
const pages = Object.fromEntries(entries.map(({ data, id }) => [id, { data }]));
8+
9+
export const { getStaticPaths, GET } = await OGImageRoute({
10+
pages,
11+
param: "slug",
12+
getImageOptions: (_id, page: (typeof pages)[number]) => {
13+
return {
14+
title: page.data.title,
15+
description: page.data.description,
16+
bgGradient: [[225, 225, 225]],
17+
padding: 90,
18+
logo: {
19+
path: "./src/pages/og/_images/logo.svg",
20+
},
21+
fonts: [
22+
"./src/fonts/AtkinsonHyperlegibleSoft-Regular.ttf",
23+
"./src/fonts/AtkinsonHyperlegibleSoft-Bold.ttf",
24+
],
25+
font: {
26+
title: {
27+
color: [17, 17, 17],
28+
size: 70,
29+
weight: "Bold",
30+
families: ["Atkinson Hyperlegible Soft", "Helvetica", "sans-serif"],
31+
},
32+
description: {
33+
color: [17, 17, 17],
34+
size: 40,
35+
weight: "Normal",
36+
families: ["Atkinson Hyperlegible Soft", "Helvetica", "sans-serif"],
37+
},
38+
},
39+
};
40+
},
41+
});

docs/src/pages/og/_images/logo.svg

3.63 KB
Loading

docs/src/routeData.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineRouteMiddleware } from "@astrojs/starlight/route-data";
2+
3+
export const onRequest = defineRouteMiddleware((context) => {
4+
const ogImageUrl = new URL(
5+
`/og/${context.locals.starlightRoute.id || "index"}.png`,
6+
context.site,
7+
);
8+
9+
const { head } = context.locals.starlightRoute;
10+
11+
head.push({
12+
tag: "meta",
13+
attrs: { property: "og:image", content: ogImageUrl.href },
14+
});
15+
head.push({
16+
tag: "meta",
17+
attrs: { name: "twitter:image", content: ogImageUrl.href },
18+
});
19+
});

pnpm-lock.yaml

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)