From c7edf035c114ac977e34ea9385b565670faeaec0 Mon Sep 17 00:00:00 2001 From: grassblock Date: Sun, 25 May 2025 23:22:51 +0800 Subject: [PATCH] fix: don't generate featured image when there are images in the article --- src/pages/post/[slug]/featured.png.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/pages/post/[slug]/featured.png.js b/src/pages/post/[slug]/featured.png.js index 449cf73..bcd4371 100644 --- a/src/pages/post/[slug]/featured.png.js +++ b/src/pages/post/[slug]/featured.png.js @@ -20,14 +20,22 @@ async function getExternalImage(post) { return matchedImage_?.src; } +// Function to check for images in markdown without rendering +function checkForImages(markdownContent) { + // Match markdown image syntax ![alt](url) or HTML tags + const imageRegex = /!\[.*?]\(.*?\)|/g; + return imageRegex.test(markdownContent); +} + // This function dynamically generates og:images for posts that don't have a featured image export async function GET({ props }) { const {post} = props; const ExternalImageURL = await getExternalImage(post); + const hasImage = checkForImages(post.body); try { // Check if a custom cover image already exists - if (post.data.cover || ExternalImageURL) { - // Redirect to the existing image + if (post.data.cover || ExternalImageURL || hasImage) { + // set it to empty to prevent the image generation return new Response(null); }