From f0c9617fc7ff0a68f79db35910eebc6d3967701e Mon Sep 17 00:00:00 2001
From: JuanM04 <me@juanm04.com>
Date: Wed, 8 Feb 2023 17:22:05 -0300
Subject: [PATCH] BREAKING CHANGE: Adds `base` to all absolute links

---
 src/node/markdown/plugins/link.ts | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/src/node/markdown/plugins/link.ts b/src/node/markdown/plugins/link.ts
index 4688f4c0c939..2a6072e8451b 100644
--- a/src/node/markdown/plugins/link.ts
+++ b/src/node/markdown/plugins/link.ts
@@ -36,15 +36,22 @@ export const linkPlugin = (
           pushLink(url, env)
         }
         hrefAttr[1] = url.replace(PATHNAME_PROTOCOL_RE, '')
-      } else if (
-        // internal anchor links
-        !url.startsWith('#') &&
-        // mail links
-        !url.startsWith('mailto:') &&
-        // links to files (other than html/md)
-        !/\.(?!html|md)\w+($|\?)/i.test(url)
-      ) {
-        normalizeHref(hrefAttr, env)
+      } else {
+        if (
+          // internal anchor links
+          !url.startsWith('#') &&
+          // mail links
+          !url.startsWith('mailto:') &&
+          // links to files (other than html/md)
+          !/\.(?!html|md)\w+($|\?)/i.test(url)
+        ) {
+          normalizeHref(hrefAttr, env)
+        }
+
+        // append base to internal (non-relative) urls
+        if (hrefAttr[1].startsWith('/')) {
+          hrefAttr[1] = `${base}${hrefAttr[1]}`.replace(/\/+/g, '/')
+        }
       }
 
       // encode vite-specific replace strings in case they appear in URLs
@@ -90,11 +97,6 @@ export const linkPlugin = (
     // export it for existence check
     pushLink(url.replace(/\.html$/, ''), env)
 
-    // append base to internal (non-relative) urls
-    if (url.startsWith('/')) {
-      url = `${base}${url}`.replace(/\/+/g, '/')
-    }
-
     // markdown-it encodes the uri
     hrefAttr[1] = decodeURI(url)
   }