From 65032ab2fc9850f7458980a5c6d2f4b7e2a51d44 Mon Sep 17 00:00:00 2001 From: grassblock Date: Wed, 14 May 2025 19:26:52 +0800 Subject: [PATCH] feat: basic og:image support (custom & featured.*) --- src/pages/post/[...slug].astro | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/pages/post/[...slug].astro b/src/pages/post/[...slug].astro index f691369..740eba7 100644 --- a/src/pages/post/[...slug].astro +++ b/src/pages/post/[...slug].astro @@ -2,6 +2,7 @@ import Layout from '../../layouts/Layout.astro'; import { getCollection } from 'astro:content'; import Comments from "../../components/Comments.astro"; +import {getImage} from "astro:assets"; export async function getStaticPaths() { const blogEntries = await getCollection('posts'); @@ -13,8 +14,15 @@ export async function getStaticPaths() { const { entry } = Astro.props; const { Content } = await entry.render(); + const slug = Astro.params.slug; -const cover = entry.data.cover +const featuredImages = import.meta.glob(`/src/content/posts/*/featured.*`,{import:'default',eager:true}); +const matchedImage = Object.keys(featuredImages).find(path => path.includes(slug)); +let matchedImage_src; +if (matchedImage) { + matchedImage_src = await getImage({src: featuredImages[matchedImage], format: 'webp'}) || null; +} +const cover = entry.data.cover || matchedImage_src?.src || `/post/${slug}/og.webp`; //|| `/posts/${slug}/featured.webp` || `/posts/${slug}/featured.png` || `/posts/${slug}/featured.jpg` ---