Skip to content

Commit dcf2941

Browse files
authored
fix(build): prepend base to all internal non-relative links (#1908)
BREAKING CHANGE: `base` is now appended to all internal (non-relative) links, including any reference to a file present in the public directory. If you want the earlier behavior for such links, use absolute links.
1 parent dd0c4c6 commit dcf2941

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/node/markdown/plugins/link.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,22 @@ export const linkPlugin = (
3636
pushLink(url, env)
3737
}
3838
hrefAttr[1] = url.replace(PATHNAME_PROTOCOL_RE, '')
39-
} else if (
40-
// internal anchor links
41-
!url.startsWith('#') &&
42-
// mail links
43-
!url.startsWith('mailto:') &&
44-
// links to files (other than html/md)
45-
!/\.(?!html|md)\w+($|\?)/i.test(url)
46-
) {
47-
normalizeHref(hrefAttr, env)
39+
} else {
40+
if (
41+
// internal anchor links
42+
!url.startsWith('#') &&
43+
// mail links
44+
!url.startsWith('mailto:') &&
45+
// links to files (other than html/md)
46+
!/\.(?!html|md)\w+($|\?)/i.test(url)
47+
) {
48+
normalizeHref(hrefAttr, env)
49+
}
50+
51+
// append base to internal (non-relative) urls
52+
if (hrefAttr[1].startsWith('/')) {
53+
hrefAttr[1] = `${base}${hrefAttr[1]}`.replace(/\/+/g, '/')
54+
}
4855
}
4956

5057
// encode vite-specific replace strings in case they appear in URLs
@@ -90,11 +97,6 @@ export const linkPlugin = (
9097
// export it for existence check
9198
pushLink(url.replace(/\.html$/, ''), env)
9299

93-
// append base to internal (non-relative) urls
94-
if (url.startsWith('/')) {
95-
url = `${base}${url}`.replace(/\/+/g, '/')
96-
}
97-
98100
// markdown-it encodes the uri
99101
hrefAttr[1] = decodeURI(url)
100102
}

0 commit comments

Comments
 (0)