Skip to content

Commit d55c5ba

Browse files
committed
Migrate relative links to absolute in embedded markdown
Signed-off-by: Martin Mihálek <[email protected]>
1 parent dc43d3c commit d55c5ba

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-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

+18
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,28 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
1515

1616
while ((token = embedTokens[step++])) {
1717
const next = (function(token) {
18+
let path = token.embed.url.split('/');
19+
path.pop();
20+
path = path.join('/');
21+
1822
return text => {
1923
let embedToken;
2024
if (text) {
2125
if (token.embed.type === 'markdown') {
26+
// Resolves relative links to absolute
27+
text = text.replace(/\[([^\[\]]+)\]\(([^)]+)\)/g, x => {
28+
const linkBeginIndex = x.indexOf('(');
29+
if (x.substring(linkBeginIndex).startsWith('(.')) {
30+
return (
31+
x.substring(0, linkBeginIndex) +
32+
`(${window.location.protocol}//${window.location.host}${path}/` +
33+
x.substring(linkBeginIndex + 1, x.length - 1) +
34+
')'
35+
);
36+
}
37+
return x;
38+
});
39+
2240
embedToken = compile.lexer(text);
2341
} else if (token.embed.type === 'code') {
2442
if (token.embed.fragment) {

0 commit comments

Comments
 (0)