-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69aa118
commit 90b5480
Showing
5 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
import type { Props } from '@astrojs/starlight/props' | ||
import Default from '@astrojs/starlight/components/Head.astro' | ||
// Get the URL of the generated image for the current page using its | ||
// ID and replace the file extension with `.png`. | ||
const ogImageUrl = new URL( | ||
`/og/${Astro.props.id.replace(/\.\w+$/, '.png')}`, | ||
Astro.site, | ||
) | ||
--- | ||
|
||
<!-- Render the default <Head/> component. --> | ||
<Default {...Astro.props}><slot /></Default> | ||
|
||
<!-- Render the <meta/> tags for the Open Graph images. --> | ||
<meta property="og:image" content={ogImageUrl} /> | ||
<meta name="twitter:image" content={ogImageUrl} /> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Add Open Graph images to Starlight: https://hideoo.dev/notes/starlight-og-images | ||
|
||
import { getCollection } from 'astro:content' | ||
import { OGImageRoute } from 'astro-og-canvas' | ||
|
||
// Get all entries from the `docs` content collection. | ||
const entries = await getCollection('docs') | ||
|
||
// Map the entry array to an object with the page ID as key and the | ||
// frontmatter data as value. | ||
const pages = Object.fromEntries(entries.map(({ data, id }) => [id, { data }])) | ||
|
||
export const { getStaticPaths, GET } = OGImageRoute({ | ||
// Pass down the documentation pages. | ||
pages, | ||
// Define the name of the parameter used in the endpoint path, here `slug` | ||
// as the file is named `[...slug].ts`. | ||
param: 'slug', | ||
// Define a function called for each page to customize the generated image. | ||
getImageOptions: (_path, page: (typeof pages)[number]) => { | ||
return { | ||
// Use the page title and description as the image title and description. | ||
title: page.data.title, | ||
description: page.data.description, | ||
// Customize various colors and add a border. | ||
bgGradient: [[24, 24, 27]], | ||
border: { color: [63, 63, 70], width: 20 }, | ||
padding: 120, | ||
} | ||
}, | ||
}) |