fix: don't generate featured image when there are images in the article
This commit is contained in:
parent
510669b34c
commit
c7edf035c1
1 changed files with 10 additions and 2 deletions
|
@ -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  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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue