diff --git a/src/lib/components/lemmy/post/PostLink.svelte b/src/lib/components/lemmy/post/PostLink.svelte index 4bd702e7..6ff3edf7 100644 --- a/src/lib/components/lemmy/post/PostLink.svelte +++ b/src/lib/components/lemmy/post/PostLink.svelte @@ -2,6 +2,7 @@ import Link, { parseURL } from '$lib/components/input/Link.svelte' import { Material } from 'mono-svelte' import { Icon, Link as LinkIcon } from 'svelte-hero-icons' + import { optimizeImageURL } from './helpers' export let url: string export let thumbnail_url: string | undefined = undefined @@ -39,10 +40,10 @@ {#if thumbnail_url} @@ -27,6 +28,20 @@ export const bestImageURL = ( return post.url ?? '' } +export const optimizeImageURL = ( + urlStr: string, + width: number = 1024 +): string => { + try { + const url = new URL(urlStr) + url.searchParams.append('format', 'webp') + url.searchParams.append('thumbnail', findClosestNumber([128, 256, 512, 1024], width).toString()) + return url.toString() + } catch (e) { + return urlStr + } +} + const YOUTUBE_REGEX = /^(?:(?:https?:)?\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/ export const isYoutubeLink = (url?: string): RegExpMatchArray | null => { diff --git a/src/lib/components/lemmy/post/media/PostIframe.svelte b/src/lib/components/lemmy/post/media/PostIframe.svelte index 76ac03ac..7df04d58 100644 --- a/src/lib/components/lemmy/post/media/PostIframe.svelte +++ b/src/lib/components/lemmy/post/media/PostIframe.svelte @@ -21,7 +21,7 @@ VideoCamera, type IconSource, } from 'svelte-hero-icons' - import type { IframeType, MediaType } from '../helpers' + import { optimizeImageURL, type IframeType, type MediaType } from '../helpers' import { userSettings } from '$lib/settings' export let type: IframeType = 'none' @@ -102,7 +102,7 @@ > {#if thumbnail} diff --git a/src/lib/components/lemmy/post/media/PostMedia.svelte b/src/lib/components/lemmy/post/media/PostMedia.svelte index b8b7ca65..f62e95c3 100644 --- a/src/lib/components/lemmy/post/media/PostMedia.svelte +++ b/src/lib/components/lemmy/post/media/PostMedia.svelte @@ -11,17 +11,13 @@ showImage, } from '$lib/components/ui/ExpandableImage.svelte' import { userSettings } from '$lib/settings.js' - import { isImage, isVideo } from '$lib/ui/image' import type { Post } from 'lemmy-js-client' - import { Icon, VideoCamera } from 'svelte-hero-icons' import PostIframe from './PostIframe.svelte' export let view: 'card' | 'cozy' | 'list' | 'compact' = 'cozy' export let post: Post export let type: MediaType = 'none' export let opened: boolean | undefined = undefined - - console.log {#if type == 'image'}