fix: Correctly replace urls with signed versions when fully qualified

This commit is contained in:
Tom Moor
2024-05-29 19:55:54 -04:00
parent 6fd7e755b0
commit 5e841f6b16

View File

@@ -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));
}
});
}