add protocol to link without it (#132)

* add protocol to link without it

* refactoing

* refactoring

* refactoring
This commit is contained in:
Ilya Y 2023-07-18 14:21:26 +03:00 committed by GitHub
parent b059f1024e
commit f86108ba20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -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<boolean>(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(

View File

@ -1,3 +1,3 @@
export const validateUrl = (value: string) => {
return /^((http|https):\/\/)?[^ "]+$/.test(value)
return value.includes('.') && !value.includes(' ')
}