Make URL in cover prop absolute when needed

This commit is contained in:
Erica Marigold 2024-07-06 21:21:11 +05:30
parent 472cba451a
commit dac1f75445
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
3 changed files with 20 additions and 14 deletions

View file

@ -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}`;
<div id="image-wrapper" class:list={[className, 'overflow-hidden relative']}>
<div class="transition absolute inset-0 dark:bg-black/10 bg-opacity-50 pointer-events-none"></div>
{isLocal && <img src={optimizedImg!.src} alt={alt || ""} class={imageClass} style={imageStyle}/>}
{!isLocal && <img src={url(src)} alt={alt || ""} class={imageClass} style={imageStyle}/>}
{!isLocal && <img src={isPublic ? url(src) : src} alt={alt || ""} class={imageClass} style={imageStyle}/>}
</div>

View file

@ -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);
---
<MainGridLayout banner={coverImageUrl} cover={coverImageUrl} title={entry.data.title} description={entry.data.description}>
<MainGridLayout banner={coverImageUrl} cover={(coverImageUrl && urlIsPublic(coverImageUrl)) ? url(coverImageUrl) : coverImageUrl} title={entry.data.title} description={entry.data.description}>
<script slot="head" type="application/ld+json" set:html={JSON.stringify(jsonLd)}></script>
<div class="flex w-full rounded-[var(--radius-large)] overflow-hidden relative mb-4">
<div id="post-container" class:list={["card-base z-10 px-6 md:px-9 pt-6 pb-4 relative w-full ",

View file

@ -35,3 +35,16 @@ export function getDir(path: string): string {
export function url(path: string) {
return joinUrl("", import.meta.env.BASE_URL, path);
}
export function urlIsLocal(path: string): boolean {
return !(
path.startsWith("/") ||
path.startsWith("http") ||
path.startsWith("https") ||
path.startsWith("data:")
);
}
export function urlIsPublic(path: string): boolean {
return path.startsWith("/");
}