feat: don't prepend baseurl in the og:image when the locally stored first image in the article is used
This commit is contained in:
parent
b814569bc6
commit
510669b34c
2 changed files with 3 additions and 11 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,Astro.url.origin)
|
const firstImageURL = await ExtractFirstImage(Content)
|
||||||
|
|
||||||
const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `/post/${slug}/featured.png` || '';
|
const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `/post/${slug}/featured.png` || '';
|
||||||
---
|
---
|
||||||
|
|
|
@ -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, baseUrl = '') {
|
export async function ExtractFirstImage(Content) {
|
||||||
// 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,15 +22,7 @@ export async function ExtractFirstImage(Content, baseUrl = '') {
|
||||||
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
|
||||||
if (!firstImageUrl) {
|
firstImageUrl = node.attributes.src;
|
||||||
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