Skip to content

Commit c8bd307

Browse files
committed
docs: add social images to the website pages
Links to the website showed no image in social networks and chats: the pages had no og:image. Every page now gets one with its title and description, generated at build time with astro-og-canvas and the Inter font from npm, so the build fetches nothing.
1 parent 6911b42 commit c8bd307

7 files changed

Lines changed: 131 additions & 0 deletions

File tree

‎pnpm-lock.yaml‎

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

‎website/astro.config.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export default defineConfig({
106106
PageSidebar: './src/components/PageSidebar.astro',
107107
Footer: './src/components/Footer.astro'
108108
},
109+
routeMiddleware: './src/routeData.ts',
109110
plugins: [
110111
llmsTxt({
111112
projectName: 'react-chartjs-2',

‎website/oxlint.config.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ export default defineConfig({
1818
'eslint/no-implicit-globals': 'off'
1919
}
2020
},
21+
{
22+
// The design of the social images: colors and sizes are data
23+
files: ['src/pages/og/**'],
24+
rules: {
25+
'eslint/no-magic-numbers': 'off'
26+
}
27+
},
2128
{
2229
// Served as is: a classic service worker script, not a module
2330
files: ['public/**'],

‎website/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
"@astrojs/check": "^0.9.12",
2929
"@astrojs/markdown-remark": "^7.3.10",
3030
"@astrojs/starlight": "^0.43.2",
31+
"@fontsource/inter": "^5.3.0",
3132
"astro": "^7.4.13",
33+
"astro-og-canvas": "^0.13.3",
34+
"canvaskit-wasm": "^0.42.0",
3235
"react-docgen-typescript": "^2.4.0",
3336
"starlight-llms-txt": "^0.12.0"
3437
},

‎website/src/assets/og-logo.png‎

5.83 KB
Loading
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { getCollection } from 'astro:content'
2+
import { OGImageRoute } from 'astro-og-canvas'
3+
4+
const font = './node_modules/@fontsource/inter/files/inter-latin'
5+
const pages = await getCollection('docs')
6+
7+
// Word joiners keep the name of the package on one line: Inter has no non-breaking hyphen
8+
function keepName(text: string) {
9+
return text.replaceAll('react-chartjs-2', 'react\u2060-\u2060chartjs\u2060-\u20602')
10+
}
11+
12+
// The image that social networks and chats show for a link to a page, at /og/<page id>.png
13+
export const { getStaticPaths, GET } = await OGImageRoute({
14+
pages: Object.fromEntries(pages.map(({ id, data }) => [id, data])),
15+
// The route has the extension, so `trailingSlash` does not apply to it
16+
getSlug: id => id,
17+
getImageOptions: (_id, { title, description = '' }) => ({
18+
title: keepName(title),
19+
description: keepName(description),
20+
// CanvasKit draws no SVG, so this is src/assets/logo.svg at the size the image shows it
21+
logo: {
22+
path: './src/assets/og-logo.png'
23+
},
24+
// The background and the accent of the dark theme
25+
bgGradient: [[23, 24, 28], [31, 29, 92]],
26+
border: {
27+
color: [122, 119, 255],
28+
width: 12
29+
},
30+
// The longest titles of the FAQ with their descriptions fit in the image with these sizes
31+
padding: 64,
32+
font: {
33+
title: {
34+
size: 64,
35+
lineHeight: 1.15,
36+
weight: 'Bold',
37+
families: ['Inter']
38+
},
39+
description: {
40+
color: [191, 193, 201],
41+
size: 32,
42+
lineHeight: 1.4,
43+
families: ['Inter']
44+
}
45+
},
46+
fonts: [`${font}-700-normal.woff2`, `${font}-400-normal.woff2`]
47+
})
48+
})

‎website/src/routeData.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { defineRouteMiddleware } from '@astrojs/starlight/route-data'
2+
3+
export const onRequest = defineRouteMiddleware(({ locals, site }) => {
4+
const { id, head } = locals.starlightRoute
5+
6+
// The 404 page of Starlight is not in the collection, so src/pages/og has no image for it
7+
if (id === '404') {
8+
return
9+
}
10+
11+
// The home page has the id `index` in the collection
12+
const image = new URL(`/og/${id || 'index'}.png`, site).href
13+
14+
head.push({
15+
tag: 'meta',
16+
attrs: {
17+
property: 'og:image',
18+
content: image
19+
}
20+
})
21+
})

0 commit comments

Comments
 (0)