-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(og-image): ✨ add og-image for posts that have cover
- Loading branch information
1 parent
4fcc832
commit 4cce346
Showing
3 changed files
with
33 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import svgPathToName from '@utils/svgPathToName'; | ||
import type { APIRoute, GetStaticPaths } from 'astro'; | ||
import { getCollection, type CollectionEntry } from 'astro:content'; | ||
import sharp from 'sharp'; | ||
|
||
export const getStaticPaths = (async () => { | ||
const blogEntries = await getCollection('posts'); | ||
return blogEntries | ||
.filter((i) => !!i.data.image) | ||
.map((entry) => ({ | ||
params: { slug: svgPathToName(entry.data.image!.src) }, | ||
props: entry, | ||
})); | ||
}) satisfies GetStaticPaths; | ||
|
||
export const GET: APIRoute<CollectionEntry<'posts'>> = async ({ props }) => { | ||
const image = await sharp(`./dist${props.data.image!.src}`) | ||
.resize(1200, 630, { fit: 'contain' }) | ||
.png() | ||
.toBuffer(); | ||
return new Response(image.buffer); | ||
}; |
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,5 @@ | ||
const svgNameReg = /[. \w-]+(?=\.svg)/; | ||
|
||
const svgPathToName = (path: string) => svgNameReg.exec(path)![0]; | ||
|
||
export default svgPathToName; |