mirror of
https://github.com/docmost/docmost.git
synced 2025-11-13 08:14:06 +10:00
fix local attachment paths in exports (#1013)
This commit is contained in:
@ -21,7 +21,7 @@ import {
|
|||||||
getProsemirrorContent,
|
getProsemirrorContent,
|
||||||
PageExportTree,
|
PageExportTree,
|
||||||
replaceInternalLinks,
|
replaceInternalLinks,
|
||||||
updateAttachmentUrls,
|
updateAttachmentUrlsToLocalPaths,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
import { PageRepo } from '@docmost/db/repos/page/page.repo';
|
import { PageRepo } from '@docmost/db/repos/page/page.repo';
|
||||||
import { Node } from '@tiptap/pm/model';
|
import { Node } from '@tiptap/pm/model';
|
||||||
@ -193,7 +193,7 @@ export class ExportService {
|
|||||||
|
|
||||||
if (includeAttachments) {
|
if (includeAttachments) {
|
||||||
await this.zipAttachments(updatedJsonContent, page.spaceId, folder);
|
await this.zipAttachments(updatedJsonContent, page.spaceId, folder);
|
||||||
updatedJsonContent = updateAttachmentUrls(updatedJsonContent);
|
updatedJsonContent = updateAttachmentUrlsToLocalPaths(updatedJsonContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
const pageTitle = getPageTitle(page.title);
|
const pageTitle = getPageTitle(page.title);
|
||||||
|
|||||||
@ -62,17 +62,30 @@ export function isAttachmentNode(nodeType: string) {
|
|||||||
return attachmentNodeTypes.includes(nodeType);
|
return attachmentNodeTypes.includes(nodeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateAttachmentUrls(prosemirrorJson: any) {
|
export function updateAttachmentUrlsToLocalPaths(prosemirrorJson: any) {
|
||||||
const doc = jsonToNode(prosemirrorJson);
|
const doc = jsonToNode(prosemirrorJson);
|
||||||
|
if (!doc) return null;
|
||||||
|
|
||||||
|
// Helper function to replace specific URL prefixes
|
||||||
|
const replacePrefix = (url: string): string => {
|
||||||
|
const prefixes = ['/files', '/api/files'];
|
||||||
|
for (const prefix of prefixes) {
|
||||||
|
if (url.startsWith(prefix)) {
|
||||||
|
return url.replace(prefix, 'files');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
};
|
||||||
|
|
||||||
doc?.descendants((node: Node) => {
|
doc?.descendants((node: Node) => {
|
||||||
if (isAttachmentNode(node.type.name)) {
|
if (isAttachmentNode(node.type.name)) {
|
||||||
if (node.attrs.src && node.attrs.src.startsWith('/files')) {
|
if (node.attrs.src) {
|
||||||
//@ts-expect-error
|
// @ts-ignore
|
||||||
node.attrs.src = node.attrs.src.replace('/files', 'files');
|
node.attrs.src = replacePrefix(node.attrs.src);
|
||||||
} else if (node.attrs.url && node.attrs.url.startsWith('/files')) {
|
}
|
||||||
//@ts-expect-error
|
if (node.attrs.url) {
|
||||||
node.attrs.url = node.attrs.url.replace('/files', 'files');
|
// @ts-ignore
|
||||||
|
node.attrs.url = replacePrefix(node.attrs.url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user