From dac1f75445de89421003216e68afd9d0b37dde84 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 6 Jul 2024 21:21:11 +0530 Subject: [PATCH] Make URL in cover prop absolute when needed --- src/components/misc/ImageWrapper.astro | 13 ++++--------- src/pages/posts/[...slug].astro | 8 +++----- src/utils/url-utils.ts | 13 +++++++++++++ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/components/misc/ImageWrapper.astro b/src/components/misc/ImageWrapper.astro index d267c12..504f421 100644 --- a/src/components/misc/ImageWrapper.astro +++ b/src/components/misc/ImageWrapper.astro @@ -9,20 +9,15 @@ interface Props { } import path from "path"; -import { url } from "../../utils/url-utils"; +import { url, urlIsLocal, urlIsPublic } from "../../utils/url-utils"; const { id, src: img, alt, position = "center", basePath = "/" } = Astro.props; const className = Astro.props.class; const src = typeof img === "string" ? img : img.src; -const isLocal = !( - src.startsWith("/") || - src.startsWith("http") || - src.startsWith("https") || - src.startsWith("data:") -); -// const isPublic = src.startsWith("/"); +const isLocal = urlIsLocal(src); +const isPublic = urlIsPublic(src); // TODO temporary workaround for images dynamic import // https://github.com/withastro/astro/issues/3373 @@ -43,6 +38,6 @@ const imageStyle = `object-position: ${position}`;
{isLocal && {alt} - {!isLocal && {alt} + {!isLocal && {alt}
diff --git a/src/pages/posts/[...slug].astro b/src/pages/posts/[...slug].astro index dcfc3c4..622b73f 100644 --- a/src/pages/posts/[...slug].astro +++ b/src/pages/posts/[...slug].astro @@ -7,13 +7,14 @@ import Markdown from "@components/misc/Markdown.astro"; import I18nKey from "@i18n/i18nKey"; import { i18n } from "@i18n/translation"; import MainGridLayout from "@layouts/MainGridLayout.astro"; -import { getDir, getPostUrlBySlug } from "@utils/url-utils"; +import { getDir, getPostUrlBySlug, urlIsPublic } from "@utils/url-utils"; import { Icon } from "astro-icon/components"; import { licenseConfig } from "src/config"; import PostMetadata from "../../components/PostMeta.astro"; import ImageWrapper from "../../components/misc/ImageWrapper.astro"; import { profileConfig } from "../../config"; import { formatDateToYYYYMMDD } from "../../utils/date-utils"; +import { url } from "../../utils/url-utils"; export async function getStaticPaths() { const blogEntries = await getCollection("posts", ({ data }) => { @@ -46,7 +47,6 @@ const jsonLd = { }; let coverImageUrl: string | undefined; - if (entry.data.image) { coverImageUrl = await getImage({ src: entry.data.image, @@ -54,10 +54,8 @@ if (entry.data.image) { height: 630, }).then(img => img.src); } - -console.log(coverImageUrl); --- - +