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 cc1ee2c
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions front-end/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -975,21 +975,36 @@
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 parent = absPath.slice(0, absPath.lastIndexOf('/')) || '/';

return (
window.fileCache
.get(parent)
?.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 cc1ee2c

Please sign in to comment.