Skip to content

Commit b73768e

Browse files
committed
fix: update relative link handling
1 parent be21637 commit b73768e

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/core/render/compiler/link.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,39 @@ export const linkCompiler = ({
1212
const attrs = [];
1313
const text = this.parser.parseInline(tokens) || '';
1414
const { str, config } = getAndRemoveConfig(title);
15+
const isAbsolute = isAbsolutePath(href);
16+
const isNotCompilable = compiler._matchNotCompileLink(href);
17+
const isMailto = href.startsWith('mailto:');
18+
1519
linkTarget = config.target || linkTarget;
1620
linkRel =
1721
linkTarget === '_blank'
1822
? compiler.config.externalLinkRel || 'noopener'
1923
: '';
2024
title = str;
2125

22-
if (
23-
!isAbsolutePath(href) &&
24-
!compiler._matchNotCompileLink(href) &&
25-
!config.ignore
26-
) {
26+
if (!isAbsolute && !isNotCompilable && !config.ignore) {
2727
if (href === compiler.config.homepage) {
2828
href = 'README';
2929
}
30-
3130
href = router.toURL(href, null, router.getCurrentPath());
3231

33-
if (config.target) {
34-
href.indexOf('mailto:') !== 0 && attrs.push(`target="${linkTarget}"`);
32+
if (config.target && !isMailto) {
33+
attrs.push(`target="${linkTarget}"`);
3534
}
3635
} else {
37-
if (!isAbsolutePath(href) && href.slice(0, 2) === './') {
38-
href =
39-
document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '') + href;
36+
if (!isAbsolute && !isNotCompilable && href.startsWith('./')) {
37+
href = router
38+
.toURL(href, null, router.getCurrentPath())
39+
.replace(/^#\//, '/');
40+
}
41+
42+
if (!isMailto) {
43+
attrs.push(`target="${linkTarget}"`);
44+
if (linkRel !== '') {
45+
attrs.push(`rel="${linkRel}"`);
46+
}
4047
}
41-
attrs.push(href.indexOf('mailto:') === 0 ? '' : `target="${linkTarget}"`);
42-
attrs.push(
43-
href.indexOf('mailto:') === 0
44-
? ''
45-
: linkRel !== ''
46-
? ` rel="${linkRel}"`
47-
: '',
48-
);
4948
}
5049

5150
if (config.disabled) {

0 commit comments

Comments
 (0)