Skip to content

Commit

Permalink
tried adding opengraph to the docs site
Browse files Browse the repository at this point in the history
  • Loading branch information
harshau007 committed Nov 18, 2024
1 parent 73b41d9 commit 77c6179
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
4 changes: 4 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export default defineConfig({
logo: {
src: "./src/assets/alg-logo.png",
},
components: {
// Relative path to the custom component.
Head: "./src/components/Head.astro",
},
social: {
github: "https://github.com/arch-linux-gui",
},
Expand Down
60 changes: 55 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/check": "^0.9.4",
"@astrojs/starlight": "^0.29.0",
"astro": "^4.16.10",
"astro-og-canvas": "^0.5.4",
"sharp": "^0.32.5",
"@astrojs/check": "^0.9.4",
"typescript": "^5.6.3"
}
}
18 changes: 18 additions & 0 deletions src/components/Head.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
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} />
29 changes: 29 additions & 0 deletions src/pages/og/[...slug].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { OGImageRoute } from "astro-og-canvas";
import { getCollection } from "astro:content";

// 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,
};
},
});

0 comments on commit 77c6179

Please sign in to comment.