diff --git a/src/lib/components/lemmy/post/helpers.ts b/src/lib/components/lemmy/post/helpers.ts index 813057bf..803f0581 100644 --- a/src/lib/components/lemmy/post/helpers.ts +++ b/src/lib/components/lemmy/post/helpers.ts @@ -1,7 +1,7 @@ import { client, getInstance } from '$lib/lemmy.js' import type { View } from '$lib/settings' import { isImage, isVideo } from '$lib/ui/image' -import { findClosestNumber } from '$lib/util' +import { canParseUrl, findClosestNumber } from '$lib/util' import type { CommentView, PersonView, Post, PostView } from 'lemmy-js-client' export const isCommentMutable = (comment: CommentView, me: PersonView) => @@ -67,7 +67,7 @@ export const mediaType = (url?: string, view: View = 'cozy'): MediaType => { if (isImage(url)) return 'image' if (isVideo(url)) return 'iframe' if (isYoutubeLink(url)) return 'iframe' - if (URL.canParse(url)) return 'embed' + if (canParseUrl(url)) return 'embed' return 'none' } diff --git a/src/lib/util.ts b/src/lib/util.ts index 8a7071b3..fce3daad 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -182,3 +182,12 @@ export const instanceToURL = (input: string) => input.startsWith('http://') || input.startsWith('https://') ? input : `https://${input}` + +export function canParseUrl(url: string): boolean { + try { + new URL(url) + return true + } catch (e) { + return false + } +} diff --git a/src/routes/comment/[instance]/[id]/+page.ts b/src/routes/comment/[instance]/[id]/+page.ts index 5d49649b..30b373ca 100644 --- a/src/routes/comment/[instance]/[id]/+page.ts +++ b/src/routes/comment/[instance]/[id]/+page.ts @@ -1,15 +1,18 @@ import { profile } from '$lib/auth.js' -import { getClient } from '$lib/lemmy.js' +import { client } from '$lib/lemmy.js' import { redirect } from '@sveltejs/kit' import { get } from 'svelte/store' export async function load({ params, fetch }) { - const comment = await getClient(undefined, fetch).getComment({ + const comment = await client({ + instanceURL: params.instance, + func: fetch, + }).getComment({ id: Number(params.id), }) redirect( 302, - `/post/${params.instance}/${comment.comment_view.post.id}?thread=${comment.comment_view.comment.path}#${comment.comment_view.comment.id}` - ); + `/post/${params.instance}/${comment.comment_view.post.id}?thread=${comment.comment_view.comment.path}#${comment.comment_view.comment.id}` + ) }