mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-14 07:04:45 -04:00
Use URL constructor instead of regex to check for valid URL
Fixes #545 Co-authored-by: Yannick Bungers <git@innay.de> Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
a160d81fe3
commit
bd11faa203
1 changed files with 4 additions and 9 deletions
|
@ -178,16 +178,11 @@ function slugifyWithUTF8 (text) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isValidURL (str) {
|
export function isValidURL (str) {
|
||||||
const pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
|
try {
|
||||||
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
|
const url = new URL(str)
|
||||||
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
|
return ['http:', 'https:'].includes(url.protocol)
|
||||||
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
|
} catch (e) {
|
||||||
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
|
|
||||||
'(\\#[-a-z\\d_]*)?$', 'i') // fragment locator
|
|
||||||
if (!pattern.test(str)) {
|
|
||||||
return false
|
return false
|
||||||
} else {
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue