fix: update relative link handling (#2579)

* fix: update relative link handling

* fix: To have the relative path evaluated also if the link needs to be not compiled.
This commit is contained in:
Luffy 2025-08-30 11:25:39 +08:00 committed by GitHub
parent 2a49bd0aa4
commit eeacfcce2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 26 deletions

View File

@ -12,6 +12,10 @@ 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'
@ -19,33 +23,28 @@ export const linkCompiler = ({
: '';
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(/^#\//, '/');
}
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) {

View File

@ -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>"`,
);
});
@ -241,7 +241,7 @@ 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>"`,
);
});
@ -249,7 +249,7 @@ describe('render', 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>"`,
);
});
@ -257,7 +257,7 @@ describe('render', 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>"`,
);
});
@ -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>"`,
);
});
@ -285,7 +285,7 @@ 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>"`,
);
});
@ -293,7 +293,7 @@ describe('render', 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>"`,
);
});
});