From 85104048ff2154a7bcf2a1e2de70a8038f182356 Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Fri, 25 Jul 2025 12:44:43 +0800 Subject: [PATCH 1/2] fix: update relative link handling --- src/core/render/compiler/link.js | 37 ++++++++++++++++---------------- test/integration/render.test.js | 14 ++++++------ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/core/render/compiler/link.js b/src/core/render/compiler/link.js index be6f31cb7..5c7f036e9 100644 --- a/src/core/render/compiler/link.js +++ b/src/core/render/compiler/link.js @@ -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 && !isNotCompilable && 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) { diff --git a/test/integration/render.test.js b/test/integration/render.test.js index 210fcc56d..cef319b01 100644 --- a/test/integration/render.test.js +++ b/test/integration/render.test.js @@ -229,7 +229,7 @@ describe('render', function () { const output = window.marked('[alt text](http://url)'); expect(output).toMatchInlineSnapshot( - '"

alt text

"', + `"

alt text

"`, ); }); @@ -241,7 +241,7 @@ describe('render', function () { const output = window.marked('[alt text](http://www.example.com)'); expect(output).toMatchInlineSnapshot( - '"

alt text

"', + `"

alt text

"`, ); }); @@ -249,7 +249,7 @@ describe('render', function () { const output = window.marked("[alt text](http://url ':disabled')"); expect(output).toMatchInlineSnapshot( - '"

alt text

"', + `"

alt text

"`, ); }); @@ -257,7 +257,7 @@ describe('render', function () { const output = window.marked("[alt text](http://url ':target=_self')"); expect(output).toMatchInlineSnapshot( - '"

alt text

"', + `"

alt text

"`, ); }); @@ -275,7 +275,7 @@ describe('render', function () { ); expect(output).toMatchInlineSnapshot( - '"

alt text

"', + `"

alt text

"`, ); }); @@ -285,7 +285,7 @@ describe('render', function () { ); expect(output).toMatchInlineSnapshot( - `"

alt text

"`, + `"

alt text

"`, ); }); @@ -293,7 +293,7 @@ describe('render', function () { const output = window.marked("[alt text](http://url ':id=someCssID')"); expect(output).toMatchInlineSnapshot( - '"

alt text

"', + `"

alt text

"`, ); }); }); From dde15942e5e6399b5b6d0545885dd269add5c715 Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Fri, 25 Jul 2025 17:48:09 +0800 Subject: [PATCH 2/2] fix: To have the relative path evaluated also if the link needs to be not compiled. --- src/core/render/compiler/link.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/render/compiler/link.js b/src/core/render/compiler/link.js index 5c7f036e9..44d2bcc2e 100644 --- a/src/core/render/compiler/link.js +++ b/src/core/render/compiler/link.js @@ -33,7 +33,7 @@ export const linkCompiler = ({ attrs.push(`target="${linkTarget}"`); } } else { - if (!isAbsolute && !isNotCompilable && href.startsWith('./')) { + if (!isAbsolute && href.startsWith('./')) { href = router .toURL(href, null, router.getCurrentPath()) .replace(/^#\//, '/');