Skip to content

Commit da8b4c3

Browse files
committed
fix: update relative link handling
1 parent 743e9cb commit da8b4c3

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
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) {

test/integration/render.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ describe('render', function () {
229229
const output = window.marked('[alt text](http://url)');
230230

231231
expect(output).toMatchInlineSnapshot(
232-
'"<p><a href="http://url" target="_blank" rel="noopener">alt text</a></p>"',
232+
`"<p><a href="http://url" target="_blank" rel="noopener">alt text</a></p>"`,
233233
);
234234
});
235235

@@ -241,23 +241,23 @@ describe('render', function () {
241241
const output = window.marked('[alt text](http://www.example.com)');
242242

243243
expect(output).toMatchInlineSnapshot(
244-
'"<p><a href="http://www.example.com" target="_blank" rel="noopener">alt text</a></p>"',
244+
`"<p><a href="http://www.example.com" target="_blank" rel="noopener">alt text</a></p>"`,
245245
);
246246
});
247247

248248
test('disabled', async function () {
249249
const output = window.marked("[alt text](http://url ':disabled')");
250250

251251
expect(output).toMatchInlineSnapshot(
252-
'"<p><a href="javascript:void(0)" target="_blank" rel="noopener" disabled>alt text</a></p>"',
252+
`"<p><a href="javascript:void(0)" target="_blank" rel="noopener" disabled>alt text</a></p>"`,
253253
);
254254
});
255255

256256
test('target for absolute path', async function () {
257257
const output = window.marked("[alt text](http://url ':target=_self')");
258258

259259
expect(output).toMatchInlineSnapshot(
260-
'"<p><a href="http://url" target="_self" >alt text</a></p>"',
260+
`"<p><a href="http://url" target="_self">alt text</a></p>"`,
261261
);
262262
});
263263

@@ -275,7 +275,7 @@ describe('render', function () {
275275
);
276276

277277
expect(output).toMatchInlineSnapshot(
278-
'"<p><a href="http://url" target="_blank" rel="noopener" class="someCssClass">alt text</a></p>"',
278+
`"<p><a href="http://url" target="_blank" rel="noopener" class="someCssClass">alt text</a></p>"`,
279279
);
280280
});
281281

@@ -285,15 +285,15 @@ describe('render', function () {
285285
);
286286

287287
expect(output).toMatchInlineSnapshot(
288-
`"<p><a href="http://url" target="_blank" rel="noopener" class="someCssClass anotherCssClass">alt text</a></p>"`,
288+
`"<p><a href="http://url" target="_blank" rel="noopener" class="someCssClass anotherCssClass">alt text</a></p>"`,
289289
);
290290
});
291291

292292
test('id', async function () {
293293
const output = window.marked("[alt text](http://url ':id=someCssID')");
294294

295295
expect(output).toMatchInlineSnapshot(
296-
'"<p><a href="http://url" target="_blank" rel="noopener" id="someCssID">alt text</a></p>"',
296+
`"<p><a href="http://url" target="_blank" rel="noopener" id="someCssID">alt text</a></p>"`,
297297
);
298298
});
299299
});

0 commit comments

Comments
 (0)