From 5e841f6b1621719490ebae667c188b9cec582904 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 29 May 2024 19:55:54 -0400 Subject: [PATCH] fix: Correctly replace urls with signed versions when fully qualified --- server/models/helpers/ProsemirrorHelper.tsx | 24 +++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) 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)); } }); }