forked from withastro/docs
-
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.
Add auto-generated OpenGraph images (withastro#1651)
* Add auto-generated OpenGraph images * Upgrade `astro-og-canvas` * Update to newer OG image library Added improved handling for some of docs’ longer titles/descriptions
- Loading branch information
Showing
6 changed files
with
126 additions
and
13 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,73 @@ | ||
import { OGImageRoute } from 'astro-og-canvas'; | ||
import { getLanguageFromURL } from '../../util'; | ||
import { rtlLanguages } from '../../i18n/languages'; | ||
|
||
export const { getStaticPaths, get } = OGImageRoute({ | ||
param: 'path', | ||
|
||
pages: await import.meta.glob('/src/pages/**/*.md'), | ||
|
||
getImageOptions: async (_, mod) => { | ||
const page = await mod(); | ||
return { | ||
title: page.frontmatter.title, | ||
description: page.frontmatter.description, | ||
dir: rtlLanguages.has(getLanguageFromURL(page.url)) ? 'rtl' : 'ltr', | ||
logo: { | ||
path: './src/docs-logo.png', | ||
size: [400], | ||
}, | ||
border: { color: [255, 93, 1], width: 20, side: 'inline-start' }, | ||
bgGradient: [ | ||
[42, 35, 62], | ||
[23, 20, 36], | ||
], | ||
font: { | ||
title: { | ||
size: 78, | ||
families: [ | ||
'Work Sans', | ||
'Noto Sans Black', | ||
'Noto Sans Arabic', | ||
'Noto Sans SC Black', | ||
'Noto Sans TC Black', | ||
'Noto Sans JP Black', | ||
], | ||
weight: 'ExtraBold', | ||
}, | ||
description: { | ||
size: 45, | ||
lineHeight: 1.25, | ||
families: [ | ||
'Work Sans', | ||
'Noto Sans', | ||
'Noto Sans Arabic', | ||
'Noto Sans SC', | ||
'Noto Sans TC', | ||
'Noto Sans JP', | ||
], | ||
weight: 'Normal', | ||
}, | ||
}, | ||
fonts: [ | ||
'https://api.fontsource.org/v1/fonts/work-sans/latin-400-normal.ttf', | ||
'https://api.fontsource.org/v1/fonts/work-sans/latin-800-normal.ttf', | ||
|
||
'https://api.fontsource.org/v1/fonts/noto-sans/cyrillic-400-normal.ttf', | ||
'https://api.fontsource.org/v1/fonts/noto-sans/cyrillic-900-normal.ttf', | ||
|
||
'https://api.fontsource.org/v1/fonts/noto-sans-sc/chinese-simplified-400-normal.ttf', | ||
'https://api.fontsource.org/v1/fonts/noto-sans-sc/chinese-simplified-900-normal.ttf', | ||
|
||
'https://api.fontsource.org/v1/fonts/noto-sans-tc/chinese-traditional-400-normal.ttf', | ||
'https://api.fontsource.org/v1/fonts/noto-sans-tc/chinese-traditional-900-normal.ttf', | ||
|
||
'https://api.fontsource.org/v1/fonts/noto-sans-jp/japanese-400-normal.ttf', | ||
'https://api.fontsource.org/v1/fonts/noto-sans-jp/japanese-900-normal.ttf', | ||
|
||
'https://api.fontsource.org/v1/fonts/noto-sans-arabic/arabic-400-normal.ttf', | ||
'https://api.fontsource.org/v1/fonts/noto-sans-arabic/arabic-800-normal.ttf', | ||
], | ||
}; | ||
}, | ||
}); |
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,22 @@ | ||
import type { GetStaticPathsOptions, GetStaticPathsResult } from 'astro'; | ||
import { getStaticPaths } from '../pages/open-graph/[...path]'; | ||
|
||
const routes = (await getStaticPaths({} as GetStaticPathsOptions)) as GetStaticPathsResult; | ||
|
||
/** All the OpenGraph image paths as generated by our `getStaticPaths`. */ | ||
const paths = new Set(routes.map(({ params }) => params.path)); | ||
|
||
/** | ||
* Get the path to the OpenGraph image for a page | ||
* @param path Pathname of the page URL. | ||
* @param isFallback Whether or not this page is displaying fallback content. | ||
* @returns Path to the OpenGraph image if found. Otherwise, `undefined`. | ||
*/ | ||
export function getOgImageUrl(path: string, isFallback: boolean): string | undefined { | ||
let imagePath = path.replace(/^\//, '').replace(/\/$/, '') + '.png'; | ||
if (isFallback) { | ||
// Replace the language segment with 'en' for fallback pages. | ||
imagePath = 'en' + imagePath.slice(imagePath.indexOf('/')); | ||
} | ||
if (paths.has(imagePath)) return '/open-graph/' + imagePath; | ||
} |