diff --git a/src/core/render/compiler/link.js b/src/core/render/compiler/link.js index be6f31cb7..44d2bcc2e 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 && 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

"`, ); }); });