md: fix image handling

This commit is contained in:
Xylight committed 2024-08-03 18:24:28 -07:00
1 parent bcc3c48f27
commit fe7898fa3c
4 files changed
+22 -12

No files matched your search

+9 -4
View File
@@ -36,10 +36,10 @@ export const optimizeImageURL = (
try {
const url = new URL(urlStr)
url.searchParams.append('format', 'webp')
if (!url.searchParams.has('format')) url.searchParams.set('format', 'webp')
if (width > 0) {
url.searchParams.append(
if (width > 0 && !url.searchParams.has('thumbnail')) {
url.searchParams.set(
'thumbnail',
findClosestNumber(
[128, 196, 256, 512, 728, 1024, 1536],
@@ -48,6 +48,10 @@ export const optimizeImageURL = (
)
}
if (width == -1) {
url.searchParams.delete('thumbnail')
}
return url.toString()
} catch (e) {
console.log(e)
@@ -74,7 +78,8 @@ export const mediaType = (url?: string, view: View = 'cozy'): MediaType => {
if (isImage(url)) return 'image'
if (isVideo(url)) return 'iframe'
if (isYoutubeLink(url)) return 'iframe'
return 'embed'
if (URL.canParse(url)) return 'embed'
return 'none'
}
return 'none'
@@ -11,11 +11,13 @@
export let title: string | undefined = undefined
export let text: string = ''
console.log(href)
$: type = mediaType(href, 'cozy')
</script>
<div
class="max-w-xl h-auto rounded-2xl p-2 border border-slate-200 dark:border-zinc-800
class="w-auto h-auto max-h-96 rounded-2xl p-2 border border-slate-200 dark:border-zinc-800
inline-block"
>
{#if type == 'video' || type == 'embed' || type == 'iframe'}
@@ -27,12 +29,15 @@
class="w-full"
/>
{:else}
<button on:click={() => showImage(href, text)}>
<button
class="inline"
on:click={() => showImage(optimizeImageURL(href, -1), text)}
>
<img
src={optimizeImageURL(href, 1024)}
{title}
alt={text}
class="rounded-lg"
class="object-contain w-auto h-auto max-h-80 inline"
/>
</button>
{/if}