feat: use relative paths for cover images

This commit is contained in:
saicaca
2023-12-28 14:37:18 +08:00
parent 197d524b53
commit 3fcb566a54
4 changed files with 17 additions and 5 deletions
+3 -2
View File
@@ -5,10 +5,11 @@ interface Props {
src: string;
class?: string;
alt?: string
basePath?: string
}
import { Image } from 'astro:assets';
const {id, src, alt} = Astro.props;
const {id, src, alt, basePath = '/'} = Astro.props;
const className = Astro.props.class;
const isLocal = !(src.startsWith('/') || src.startsWith('http') || src.startsWith('https') || src.startsWith('data:'));
@@ -18,7 +19,7 @@ const isLocal = !(src.startsWith('/') || src.startsWith('http') || src.startsWit
let img;
if (isLocal) {
const files = import.meta.glob<ImageMetadata>("../../**", { import: 'default' });
let normalizedPath = "../../" + path.normalize(src).replace(/\\/g, "/");
let normalizedPath = path.normalize(path.join("../../", basePath, src)).replace(/\\/g, "/");
img = await (files[normalizedPath])();
}