Skip to content

[WIP] Better support for relative links (e.g. download links) for non-markdown files #2300

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 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Better support for relative links
  • Loading branch information
MartenBE committed Nov 6, 2023
commit e594e39e8f6b18b892473846bbefb0cc9cabe522
1 change: 1 addition & 0 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export class Compiler {
origin.code = highlightCodeCompiler({ renderer });
origin.link = linkCompiler({
renderer,
contentBase,
router,
linkTarget,
linkRel,
Expand Down
55 changes: 33 additions & 22 deletions src/core/render/compiler/link.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { getAndRemoveConfig } from '../utils.js';
import { isAbsolutePath } from '../../router/util.js';
import { isAbsolutePath, getPath, getParentPath } from '../../router/util.js';

const GET_EXTENSION_REGEXP = /(?:\.([^.]+))?$/;
const GET_REDUNDANT_DOTS = /\/\.\//g;

export const linkCompiler = ({
renderer,
contentBase,
router,
linkTarget,
linkRel,
Expand All @@ -18,29 +22,36 @@ export const linkCompiler = ({
: '';
title = str;

if (
!isAbsolutePath(href) &&
!compilerClass._matchNotCompileLink(href) &&
!config.ignore
) {
if (href === compilerClass.config.homepage) {
href = 'README';
}
if (!config.ignore && !compilerClass._matchNotCompileLink(href)) {
if (!isAbsolutePath(href)) {
if (href === compilerClass.config.homepage) {
href = 'README';
} else {
const ext = GET_EXTENSION_REGEXP.exec(href)[1];
if (!ext || ext === 'md') {
href = router.toURL(href, null, router.getCurrentPath());
} else {
href = getPath(
contentBase,
getParentPath(router.getCurrentPath()),
href
);
}

href = router.toURL(href, null, router.getCurrentPath());
} else {
if (!isAbsolutePath(href) && href.slice(0, 2) === './') {
href =
document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '') + href;
href = href.replace(GET_REDUNDANT_DOTS, '/');
}
} else {
attrs.push(
href.indexOf('mailto:') === 0 ? '' : `target="${linkTarget}"`
);
attrs.push(
href.indexOf('mailto:') === 0
? ''
: linkRel !== ''
? ` rel="${linkRel}"`
: ''
);
}
attrs.push(href.indexOf('mailto:') === 0 ? '' : `target="${linkTarget}"`);
attrs.push(
href.indexOf('mailto:') === 0
? ''
: linkRel !== ''
? ` rel="${linkRel}"`
: ''
);
}

if (config.disabled) {
Expand Down
42 changes: 42 additions & 0 deletions test/integration/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,47 @@ describe('render', function () {
`"<p><a href=\\"http://url\\" target=\\"_blank\\" rel=\\"noopener\\" id=\\"someCssID\\">alt text</a></p>"`
);
});

test('relative link with md extension', async function () {
const output = window.marked('[alt text](test.md)');

expect(output).toMatchInlineSnapshot(
`"<p><a href=\\"#/test\\" >alt text</a></p>"`
);
});

test('relative link with md extension starting with "./"', async function () {
const output = window.marked('[alt text](./test.md)');

expect(output).toMatchInlineSnapshot(
`"<p><a href=\\"#/test\\" >alt text</a></p>"`
);
});

test('relative link with extension other than md', async function () {
const output = window.marked('[alt text](test.txt)');

expect(output).toMatchInlineSnapshot(
`"<p><a href=\\"http://127.0.0.1:3001/test.txt\\" >alt text</a></p>"`
);
});

test('relative link with extension other than md starting with "./"', async function () {
const output = window.marked('[alt text](./test.txt)');

expect(output).toMatchInlineSnapshot(
`"<p><a href=\\"http://127.0.0.1:3001/test.txt\\" >alt text</a></p>"`
);
});

test('absolute link with md extension', async function () {
const output = window.marked(
'[alt text](http://www.example.com/test.md)'
);

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