Skip to content

Commit

Permalink
Migrate relative links to absolute in embedded markdown
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Mihálek <[email protected]>
  • Loading branch information
aenniw committed Feb 23, 2020
1 parent dc43d3c commit 14c0688
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/core/render/compiler/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`);
}

Expand Down
20 changes: 19 additions & 1 deletion src/core/render/embed.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stripIndent from 'strip-indent';
import { get } from '../fetch/ajax';
import { merge } from '../util/core';
import stripIndent from 'strip-indent';

const cached = {};

Expand All @@ -15,10 +15,28 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {

while ((token = embedTokens[step++])) {
const next = (function(token) {
let path = token.embed.url.split('/');
path.pop();
path = path.join('/');

return text => {
let embedToken;
if (text) {
if (token.embed.type === 'markdown') {
// 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) {
Expand Down

0 comments on commit 14c0688

Please sign in to comment.