From da3d7812a143e3aa360845b89f573d4e1ec637dd Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Sat, 21 Oct 2023 14:37:11 +0530 Subject: [PATCH] fix: don't normalize non-html/non-http links (#3114) --- src/client/theme-default/support/utils.ts | 11 +++++++++-- src/node/markdown/plugins/link.ts | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/client/theme-default/support/utils.ts b/src/client/theme-default/support/utils.ts index 3d05e9f5..1cd168cd 100644 --- a/src/client/theme-default/support/utils.ts +++ b/src/client/theme-default/support/utils.ts @@ -21,10 +21,17 @@ export function ensureStartingSlash(path: string): string { } export function normalizeLink(url: string): string { - if (isExternal(url)) return url + const { pathname, search, hash, protocol } = new URL(url, 'http://a.com') + + if ( + isExternal(url) || + url.startsWith('#') || + !protocol.startsWith('http') || + /\.(?!html|md)\w+($|\?)/i.test(url) + ) + return url const { site } = useData() - const { pathname, search, hash } = new URL(url, 'http://a.com') const normalizedPath = pathname.endsWith('/') || pathname.endsWith('.html') diff --git a/src/node/markdown/plugins/link.ts b/src/node/markdown/plugins/link.ts index 9aa29c96..841cd4cd 100644 --- a/src/node/markdown/plugins/link.ts +++ b/src/node/markdown/plugins/link.ts @@ -38,8 +38,8 @@ export const linkPlugin = ( if ( // internal anchor links !url.startsWith('#') && - // mail links - !url.startsWith('mailto:') && + // mail/custom protocol links + new URL(url, 'http://a.com').protocol.startsWith('http') && // links to files (other than html/md) !/\.(?!html|md)\w+($|\?)/i.test(url) ) {