Compare commits
No commits in common. "c7edf035c114ac977e34ea9385b565670faeaec0" and "b814569bc638b02a70cf2ae21b60da6fde1c8e00" have entirely different histories.
c7edf035c1
...
b814569bc6
3 changed files with 13 additions and 13 deletions
|
@ -33,7 +33,7 @@ let matchedImage_src;
|
||||||
if (matchedImage && !customFeaturedImage) {
|
if (matchedImage && !customFeaturedImage) {
|
||||||
matchedImage_src = await getImage({src: featuredImages[matchedImage], format: 'webp'}) || null;
|
matchedImage_src = await getImage({src: featuredImages[matchedImage], format: 'webp'}) || null;
|
||||||
}
|
}
|
||||||
const firstImageURL = await ExtractFirstImage(Content)
|
const firstImageURL = await ExtractFirstImage(Content,Astro.url.origin)
|
||||||
|
|
||||||
const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `/post/${slug}/featured.png` || '';
|
const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `/post/${slug}/featured.png` || '';
|
||||||
---
|
---
|
||||||
|
|
|
@ -20,22 +20,14 @@ 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 || hasImage) {
|
if (post.data.cover || ExternalImageURL) {
|
||||||
// set it to empty to prevent the image generation
|
// Redirect to the existing image
|
||||||
return new Response(null);
|
return new Response(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import {getContainerRenderer as getMDXRenderer} from "@astrojs/mdx";
|
||||||
import {experimental_AstroContainer as AstroContainer} from "astro/container";
|
import {experimental_AstroContainer as AstroContainer} from "astro/container";
|
||||||
import {transform, walk} from "ultrahtml";
|
import {transform, walk} from "ultrahtml";
|
||||||
|
|
||||||
export async function ExtractFirstImage(Content) {
|
export async function ExtractFirstImage(Content, baseUrl = '') {
|
||||||
// Load MDX renderer. Other renderers for UI frameworks (e.g. React, Vue, etc.) would need adding here if you were using those.
|
// Load MDX renderer. Other renderers for UI frameworks (e.g. React, Vue, etc.) would need adding here if you were using those.
|
||||||
const renderers = await loadRenderers([getMDXRenderer()]);
|
const renderers = await loadRenderers([getMDXRenderer()]);
|
||||||
|
|
||||||
|
@ -22,7 +22,15 @@ export async function ExtractFirstImage(Content) {
|
||||||
await walk(node, (node) => {
|
await walk(node, (node) => {
|
||||||
if (node.name === "img" && node.attributes.src) {
|
if (node.name === "img" && node.attributes.src) {
|
||||||
// Store the first image URL we find
|
// Store the first image URL we find
|
||||||
firstImageUrl = node.attributes.src;
|
if (!firstImageUrl) {
|
||||||
|
firstImageUrl = node.attributes.src.startsWith("/")
|
||||||
|
? baseUrl + node.attributes.src
|
||||||
|
: node.attributes.src;
|
||||||
|
}
|
||||||
|
// Still update the src attribute if needed
|
||||||
|
if (node.attributes.src.startsWith("/")) {
|
||||||
|
node.attributes.src = baseUrl + node.attributes.src;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return node;
|
return node;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue