Skip to content

Commit 291c99b

Browse files
authored
Migrate relative links to absolute in embedded markdown (#867)
Signed-off-by: Martin Mihálek <[email protected]>
1 parent b53ea1e commit 291c99b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Diff for: src/core/render/compiler/link.js

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export const linkCompiler = ({ renderer, router, linkTarget, compilerClass }) =>
1919

2020
href = router.toURL(href, null, router.getCurrentPath());
2121
} else {
22+
if (!isAbsolutePath(href) && href.startsWith('./')) {
23+
href =
24+
document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '') + href;
25+
}
2226
attrs.push(href.indexOf('mailto:') === 0 ? '' : `target="${linkTarget}"`);
2327
}
2428

Diff for: src/core/render/embed.js

+17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
1919
let embedToken;
2020
if (text) {
2121
if (token.embed.type === 'markdown') {
22+
let path = token.embed.url.split('/');
23+
path.pop();
24+
path = path.join('/');
25+
// Resolves relative links to absolute
26+
text = text.replace(/\[([^[\]]+)\]\(([^)]+)\)/g, x => {
27+
const linkBeginIndex = x.indexOf('(');
28+
if (x.substring(linkBeginIndex).startsWith('(.')) {
29+
return (
30+
x.substring(0, linkBeginIndex) +
31+
`(${window.location.protocol}//${window.location.host}${path}/` +
32+
x.substring(linkBeginIndex + 1, x.length - 1) +
33+
')'
34+
);
35+
}
36+
return x;
37+
});
38+
2239
embedToken = compile.lexer(text);
2340
} else if (token.embed.type === 'code') {
2441
if (token.embed.fragment) {

0 commit comments

Comments
 (0)