fix: More flexible link validation in editor allows custom protocols
closes #4319
This commit is contained in:
@@ -45,10 +45,11 @@ export function isInternalUrl(href: string) {
|
||||
/**
|
||||
* Returns true if the given string is a url.
|
||||
*
|
||||
* @param url The url to check.
|
||||
* @param text The url to check.
|
||||
* @param options Parsing options.
|
||||
* @returns True if a url, false otherwise.
|
||||
*/
|
||||
export function isUrl(text: string) {
|
||||
export function isUrl(text: string, options?: { requireHostname: boolean }) {
|
||||
if (text.match(/\n/)) {
|
||||
return false;
|
||||
}
|
||||
@@ -57,7 +58,18 @@ export function isUrl(text: string) {
|
||||
const url = new URL(text);
|
||||
const blockedProtocols = ["javascript:", "file:", "vbscript:", "data:"];
|
||||
|
||||
return url.hostname !== "" && !blockedProtocols.includes(url.protocol);
|
||||
if (blockedProtocols.includes(url.protocol)) {
|
||||
return false;
|
||||
}
|
||||
if (url.hostname) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (
|
||||
url.protocol !== "" &&
|
||||
url.pathname.startsWith("//") &&
|
||||
!options?.requireHostname
|
||||
);
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
@@ -86,7 +98,7 @@ export function sanitizeUrl(url: string | null | undefined) {
|
||||
}
|
||||
|
||||
if (
|
||||
!isUrl(url) &&
|
||||
!isUrl(url, { requireHostname: false }) &&
|
||||
!url.startsWith("/") &&
|
||||
!url.startsWith("#") &&
|
||||
!url.startsWith("mailto:") &&
|
||||
|
||||
Reference in New Issue
Block a user