fix: link validation not working for embed links

This commit is contained in:
Rotimi Best
2024-12-26 20:49:08 +01:00
committed by GitHub
parent 71d87c8bd9
commit 1f1ee8afd3
@@ -1,11 +1,11 @@
<script>
import { CopyButton, Tag } from 'carbon-components-svelte';
import TrashCanIcon from 'carbon-icons-svelte/lib/TrashCan.svelte';
import IconButton from '$lib/components/IconButton/index.svelte';
import { isLessonDirty, lesson } from '$lib/components/Course/components/Lesson/store/lessons';
import TextField from '$lib/components/Form/TextField.svelte';
import { lesson, isLessonDirty } from '$lib/components/Course/components/Lesson/store/lessons';
import IconButton from '$lib/components/IconButton/index.svelte';
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
import { t } from '$lib/utils/functions/translations';
import { CopyButton, Tag } from 'carbon-components-svelte';
import TrashCanIcon from 'carbon-icons-svelte/lib/TrashCan.svelte';
let genericLinks = '';
let error = '';
@@ -37,9 +37,20 @@
}
function isValidLink(link = '') {
const linkRegex = /^(https?:\/\/)?(www\.)?([a-zA-Z]+\.com\/)([a-zA-Z0-9_-]+)/;
// Basic URL validation
if (!link) return false;
return linkRegex.test(link.trim());
try {
const url = new URL(link.trim());
return url.protocol === 'http:' || url.protocol === 'https:';
} catch {
// If URL construction fails, try prepending https://
try {
return !!new URL(`https://${link.trim()}`);
} catch {
return false;
}
}
}
function removeVideo(index = 0) {
@@ -47,7 +58,7 @@
}
</script>
<div class="w-full flex items-{error ? 'center' : 'end'} justify-between gap-5">
<div class="flex w-full items-{error ? 'center' : 'end'} justify-between gap-5">
<TextField
label={$t('course.navItem.lessons.materials.tabs.video.embed_link')}
bind:value={genericLinks}