forked from withastro/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getOgImageUrl.ts
22 lines (19 loc) · 993 Bytes
/
getOgImageUrl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
}