From f86108ba200a332d8b6b9f5b111d5e2ac7f826d5 Mon Sep 17 00:00:00 2001 From: Ilya Y <75578537+ilya-bkv@users.noreply.github.com> Date: Tue, 18 Jul 2023 14:21:26 +0300 Subject: [PATCH] add protocol to link without it (#132) * add protocol to link without it * refactoing * refactoring * refactoring --- .../Editor/TextBubbleMenu/TextBubbleMenu.tsx | 15 ++++++++++++++- src/utils/validateUrl.ts | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/components/Editor/TextBubbleMenu/TextBubbleMenu.tsx b/src/components/Editor/TextBubbleMenu/TextBubbleMenu.tsx index fb0a9c64..a49915b0 100644 --- a/src/components/Editor/TextBubbleMenu/TextBubbleMenu.tsx +++ b/src/components/Editor/TextBubbleMenu/TextBubbleMenu.tsx @@ -15,6 +15,15 @@ type BubbleMenuProps = { ref: (el: HTMLDivElement) => void } +const checkUrl = (url) => { + try { + new URL(url) + return url + } catch { + return `https://${url}` + } +} + export const TextBubbleMenu = (props: BubbleMenuProps) => { const { t } = useLocalize() const [textSizeBubbleOpen, setTextSizeBubbleOpen] = createSignal(false) @@ -59,7 +68,11 @@ export const TextBubbleMenu = (props: BubbleMenuProps) => { } const handleLinkFormSubmit = (value: string) => { - props.editor.chain().focus().setLink({ href: value }).run() + props.editor + .chain() + .focus() + .setLink({ href: checkUrl(value) }) + .run() } const currentUrl = createEditorTransaction( diff --git a/src/utils/validateUrl.ts b/src/utils/validateUrl.ts index 4578f421..9f416965 100644 --- a/src/utils/validateUrl.ts +++ b/src/utils/validateUrl.ts @@ -1,3 +1,3 @@ export const validateUrl = (value: string) => { - return /^((http|https):\/\/)?[^ "]+$/.test(value) + return value.includes('.') && !value.includes(' ') }