fix: dont use URL.canParse

This commit is contained in:
Xylight committed 2024-09-08 15:13:19 -07:00
1 parent 12ab08f2d5
commit f2b16d5669
3 files changed
+18 -6

No files matched your search

+2 -2
View File
@@ -1,7 +1,7 @@
import { client, getInstance } from '$lib/lemmy.js' import { client, getInstance } from '$lib/lemmy.js'
import type { View } from '$lib/settings' import type { View } from '$lib/settings'
import { isImage, isVideo } from '$lib/ui/image' 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' import type { CommentView, PersonView, Post, PostView } from 'lemmy-js-client'
export const isCommentMutable = (comment: CommentView, me: PersonView) => 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 (isImage(url)) return 'image'
if (isVideo(url)) return 'iframe' if (isVideo(url)) return 'iframe'
if (isYoutubeLink(url)) return 'iframe' if (isYoutubeLink(url)) return 'iframe'
if (URL.canParse(url)) return 'embed' if (canParseUrl(url)) return 'embed'
return 'none' return 'none'
} }
+9
View File
@@ -182,3 +182,12 @@ export const instanceToURL = (input: string) =>
input.startsWith('http://') || input.startsWith('https://') input.startsWith('http://') || input.startsWith('https://')
? input ? input
: `https://${input}` : `https://${input}`
export function canParseUrl(url: string): boolean {
try {
new URL(url)
return true
} catch (e) {
return false
}
}
+7 -4
View File
@@ -1,15 +1,18 @@
import { profile } from '$lib/auth.js' import { profile } from '$lib/auth.js'
import { getClient } from '$lib/lemmy.js' import { client } from '$lib/lemmy.js'
import { redirect } from '@sveltejs/kit' import { redirect } from '@sveltejs/kit'
import { get } from 'svelte/store' import { get } from 'svelte/store'
export async function load({ params, fetch }) { 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), id: Number(params.id),
}) })
redirect( redirect(
302, 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}`
); )
} }