Skip to content

fix: update relative link handling #2579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions src/core/render/compiler/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,39 @@ export const linkCompiler = ({
const attrs = [];
const text = this.parser.parseInline(tokens) || '';
const { str, config } = getAndRemoveConfig(title);
const isAbsolute = isAbsolutePath(href);
const isNotCompilable = compiler._matchNotCompileLink(href);
const isMailto = href.startsWith('mailto:');

linkTarget = config.target || linkTarget;
linkRel =
linkTarget === '_blank'
? compiler.config.externalLinkRel || 'noopener'
: '';
title = str;

if (
!isAbsolutePath(href) &&
!compiler._matchNotCompileLink(href) &&
!config.ignore
) {
if (!isAbsolute && !isNotCompilable && !config.ignore) {
if (href === compiler.config.homepage) {
href = 'README';
}

href = router.toURL(href, null, router.getCurrentPath());

if (config.target) {
href.indexOf('mailto:') !== 0 && attrs.push(`target="${linkTarget}"`);
if (config.target && !isMailto) {
attrs.push(`target="${linkTarget}"`);
}
} else {
if (!isAbsolutePath(href) && href.slice(0, 2) === './') {
href =
document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '') + href;
if (!isAbsolute && href.startsWith('./')) {
href = router
.toURL(href, null, router.getCurrentPath())
.replace(/^#\//, '/');
Comment on lines +37 to +39
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix this here by calling toURL to handle relative paths according to routerMode.

}

if (!isMailto) {
attrs.push(`target="${linkTarget}"`);
if (linkRel !== '') {
attrs.push(`rel="${linkRel}"`);
}
}
attrs.push(href.indexOf('mailto:') === 0 ? '' : `target="${linkTarget}"`);
attrs.push(
href.indexOf('mailto:') === 0
? ''
: linkRel !== ''
? ` rel="${linkRel}"`
: '',
);
}

if (config.disabled) {
Expand Down
14 changes: 7 additions & 7 deletions test/integration/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('render', function () {
const output = window.marked('[alt text](http://url)');

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

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

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

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

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

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

expect(output).toMatchInlineSnapshot(
'"<p><a href="http://url" target="_self" >alt text</a></p>"',
`"<p><a href="http://url" target="_self">alt text</a></p>"`,
);
});

Expand All @@ -275,7 +275,7 @@ describe('render', function () {
);

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

Expand All @@ -285,15 +285,15 @@ describe('render', function () {
);

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

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

expect(output).toMatchInlineSnapshot(
'"<p><a href="http://url" target="_blank" rel="noopener" id="someCssID">alt text</a></p>"',
`"<p><a href="http://url" target="_blank" rel="noopener" id="someCssID">alt text</a></p>"`,
);
});
});
Expand Down
Loading