fix: error with MdImage & invalid url

This commit is contained in:
Xylight committed 2025-10-28 20:49:22 -07:00
1 parent 537e1c4cea
commit 3942535831
2 files changed
+15 -4

No files matched your search

@@ -27,7 +27,7 @@
</script> </script>
<div <div
class="w-auto h-auto max-h-96 rounded-xl border border-slate-200 dark:border-zinc-800 inline-block group" class="w-auto h-auto max-h-96 rounded-2xl border border-slate-200 dark:border-zinc-800 inline-block group"
> >
{#if loaded} {#if loaded}
{#if type == 'video' || type == 'embed' || type == 'iframe'} {#if type == 'video' || type == 'embed' || type == 'iframe'}
@@ -40,7 +40,7 @@
/> />
{:else} {:else}
<button <button
class="inline cursor-pointer bg-slate-200 dark:bg-zinc-800 rounded-[inherit]" class="inline cursor-pointer bg-slate-200 dark:bg-zinc-900 rounded-[inherit]"
onclick={() => showImage(optimizeImageURL(href, -1))} onclick={() => showImage(optimizeImageURL(href, -1))}
> >
<img <img
@@ -51,7 +51,7 @@
height={300} height={300}
class={[ class={[
'object-contain w-auto h-auto max-h-80 inline rounded-[inherit] group-hover:scale-98 group-active:scale-95', 'object-contain w-auto h-auto max-h-80 inline rounded-[inherit] group-hover:scale-98 group-active:scale-95',
'transition-transform ease-cubic', 'transition-transform ease-cubic duration-300',
]} ]}
/> />
</button> </button>
+12 -1
View File
@@ -31,7 +31,12 @@ export const optimizeImageURL = (
format: 'avif' | 'webp' | null = 'webp', format: 'avif' | 'webp' | null = 'webp',
): string => { ): string => {
try { try {
const url = new URL(urlStr) let url: URL
try {
url = new URL(urlStr)
} catch {
return urlStr
}
if (format) url.searchParams.set('format', format) if (format) url.searchParams.set('format', format)
@@ -70,6 +75,12 @@ export type IframeType = 'youtube' | 'video' | 'none'
export function mediaType(url?: string): MediaType { export function mediaType(url?: string): MediaType {
if (!url) return 'none' if (!url) return 'none'
try {
new URL(url)
} catch {
return 'none'
}
if (isImage(url)) return 'image' if (isImage(url)) return 'image'
if (isVideo(url)) return 'iframe' if (isVideo(url)) return 'iframe'
if (isYoutubeLink(url)) return 'iframe' if (isYoutubeLink(url)) return 'iframe'