Migrate relative links to absolute in embedded markdown (#867)

Signed-off-by: Martin Mihálek <aenniw@gmail.com>
This commit is contained in:
Martin Mihálek
2020-03-09 14:21:46 +05:30
committed by GitHub
parent b53ea1e304
commit 291c99bc38
2 changed files with 21 additions and 0 deletions
+4
View File
@@ -19,6 +19,10 @@ export const linkCompiler = ({ renderer, router, linkTarget, compilerClass }) =>
href = router.toURL(href, null, router.getCurrentPath());
} else {
if (!isAbsolutePath(href) && href.startsWith('./')) {
href =
document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '') + href;
}
attrs.push(href.indexOf('mailto:') === 0 ? '' : `target="${linkTarget}"`);
}
+17
View File
@@ -19,6 +19,23 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
let embedToken;
if (text) {
if (token.embed.type === 'markdown') {
let path = token.embed.url.split('/');
path.pop();
path = path.join('/');
// Resolves relative links to absolute
text = text.replace(/\[([^[\]]+)\]\(([^)]+)\)/g, x => {
const linkBeginIndex = x.indexOf('(');
if (x.substring(linkBeginIndex).startsWith('(.')) {
return (
x.substring(0, linkBeginIndex) +
`(${window.location.protocol}//${window.location.host}${path}/` +
x.substring(linkBeginIndex + 1, x.length - 1) +
')'
);
}
return x;
});
embedToken = compile.lexer(text);
} else if (token.embed.type === 'code') {
if (token.embed.fragment) {