From a24c037b322dbdc56cbaae3d3fe6f5fa1e819972 Mon Sep 17 00:00:00 2001 From: Vijay Aravamudhan Date: Sat, 7 May 2022 13:17:26 -0500 Subject: New fix for fixing URLs with '//' (#105) --- src/helpers/url-helpers.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/helpers/url-helpers.ts') diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts index ef2805595..1eda325da 100644 --- a/src/helpers/url-helpers.ts +++ b/src/helpers/url-helpers.ts @@ -15,9 +15,6 @@ export function isValidExternalURL(url: string | URL) { } catch { return false; } - if (url.toString().endsWith('/')) { - return false; - } const isAllowed = ALLOWED_PROTOCOLS.includes(parsedUrl.protocol); @@ -26,6 +23,10 @@ export function isValidExternalURL(url: string | URL) { return isAllowed; } +export function fixUrl(url: string | URL) { + return url.toString().replaceAll('//', '/').replace('http:/', 'http://').replace('https:/', 'https://').replace('file:/', 'file://'); +} + export function isValidFileUrl(path: string) { return path.startsWith('file') && existsSync(new URL(path)); } @@ -40,8 +41,9 @@ export function openExternalUrl( url: string | URL, skipValidityCheck: boolean = false, ) { - debug('Open url:', url, 'with skipValidityCheck:', skipValidityCheck); - if (skipValidityCheck || isValidExternalURL(url)) { - shell.openExternal(url.toString()); + const fixedUrl = fixUrl(url.toString()); + debug('Open url:', fixedUrl, 'with skipValidityCheck:', skipValidityCheck); + if (skipValidityCheck || isValidExternalURL(fixedUrl)) { + shell.openExternal(fixedUrl.toString()); } } -- cgit v1.2.3-70-g09d2