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