Skip to content

Commit

Permalink
feat: check cache for relative paths in markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
vcheckzen committed Oct 8, 2024
1 parent 7995dae commit a038e3f
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions front-end/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -975,21 +975,38 @@
window.fileCache.set(path, text);
}

// SCF_GATEWAY must contains protocol and resources can't be in encrypted path
const renderer = new marked.Renderer();
renderer.image = (href, title, text) => {
if (href.startsWith('./') || href.startsWith('../')) {
href = new URL(href, window.GLOBAL_CONFIG.SCF_GATEWAY + path).href;
}
return `<img src="${href}" alt="${text}">`;
};
renderer.link = (href, title, text) => {
if (href.startsWith('./') || href.startsWith('../')) {
href = new URL(href, window.GLOBAL_CONFIG.SCF_GATEWAY + path).href;
// It is not possible to load resources that are not cached and in an encrypted path.
const transformHref = (href) => {
if (href.startsWith('http') || href.startsWith('//')) {
return href;
}
return `<a href="${href}">${text}</a>`;

const dummyBase = new URL(
window.backFordwardCache.current + '/',
location
);
const absPath = new URL(href, dummyBase).href.slice(
location.origin.length
);

const containingPath = (path) =>
path.slice(0, path.lastIndexOf('/')) || '/';

return (
window.fileCache
.get(containingPath(absPath))
?.files.find(({ name }) => name === absPath.split('/').pop())
?.url ||
window.GLOBAL_CONFIG.SCF_GATEWAY.replace('/$', '') + absPath
);
};

const renderer = new marked.Renderer();
renderer.image = (href, title, text) =>
`<img src="${transformHref(href)}" alt="${text}">`;
renderer.link = (href, title, text) =>
`<a href="${transformHref(href)}">${text}</a>`;

text = marked.parse(text, {
gfm: true,
renderer: renderer,
Expand Down

0 comments on commit a038e3f

Please sign in to comment.