diff --git a/server/models/helpers/ProsemirrorHelper.tsx b/server/models/helpers/ProsemirrorHelper.tsx index 2a8dd24ae..5e9bb6397 100644 --- a/server/models/helpers/ProsemirrorHelper.tsx +++ b/server/models/helpers/ProsemirrorHelper.tsx @@ -203,16 +203,32 @@ export class ProsemirrorHelper { const json = doc.toJSON() as ProsemirrorData; + function getMapping(href: string) { + let relativeHref; + + try { + const url = new URL(href); + relativeHref = url.toString().substring(url.origin.length); + } catch { + // Noop: Invalid url. + } + + return ( + mapping[href] || + (relativeHref ? mapping[relativeHref] : undefined) || + href + ); + } + function replaceAttachmentUrls(node: ProsemirrorData) { if (node.attrs?.src) { - node.attrs.src = mapping[node.attrs.src as string] || node.attrs.src; + node.attrs.src = getMapping(String(node.attrs.src)); } else if (node.attrs?.href) { - node.attrs.href = mapping[node.attrs.href as string] || node.attrs.href; + node.attrs.href = getMapping(String(node.attrs.href)); } else if (node.marks) { node.marks.forEach((mark) => { if (mark.attrs?.href) { - mark.attrs.href = - mapping[mark.attrs.href as string] || mark.attrs.href; + mark.attrs.href = getMapping(String(mark.attrs.href)); } }); }