md: fix image handling
This commit is contained in:
1 parent
bcc3c48f27
commit
fe7898fa3c
4 files changed
+22
-12
No files matched your search
@@ -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}
|
||||
|
||||
+2
-2
@@ -2,7 +2,7 @@ export const isImage = (url: string | undefined) => {
|
||||
try {
|
||||
if (!url) return false
|
||||
|
||||
return /\.(jpeg|jpg|gif|png|svg|bmp|webp)$/i.test(url)
|
||||
return /\.(jpeg|jpg|gif|png|svg|bmp|webp|avif)/i.test(url)
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
@@ -12,7 +12,7 @@ export const isVideo = (url: string | undefined) => {
|
||||
try {
|
||||
if (!url) return false
|
||||
|
||||
return /\.(mp4|mov|webm|mkv)$/i.test(url)
|
||||
return /\.(mp4|mov|webm|mkv|avi)/i.test(url)
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user