fix: don't generate featured image when there are images in the article

This commit is contained in:
grassblock 2025-05-25 23:22:51 +08:00
parent 510669b34c
commit c7edf035c1

View file

@ -20,14 +20,22 @@ async function getExternalImage(post) {
return matchedImage_?.src; return matchedImage_?.src;
} }
// Function to check for images in markdown without rendering
function checkForImages(markdownContent) {
// Match markdown image syntax ![alt](url) or HTML <img> tags
const imageRegex = /!\[.*?]\(.*?\)|<img.*?src=["'].*?["'].*?>/g;
return imageRegex.test(markdownContent);
}
// This function dynamically generates og:images for posts that don't have a featured image // This function dynamically generates og:images for posts that don't have a featured image
export async function GET({ props }) { export async function GET({ props }) {
const {post} = props; const {post} = props;
const ExternalImageURL = await getExternalImage(post); const ExternalImageURL = await getExternalImage(post);
const hasImage = checkForImages(post.body);
try { try {
// Check if a custom cover image already exists // Check if a custom cover image already exists
if (post.data.cover || ExternalImageURL) { if (post.data.cover || ExternalImageURL || hasImage) {
// Redirect to the existing image // set it to empty to prevent the image generation
return new Response(null); return new Response(null);
} }