diff --git a/apps/web/src/utils/streams/export-stream.ts b/apps/web/src/utils/streams/export-stream.ts index 8a2481da3..b95a147ad 100644 --- a/apps/web/src/utils/streams/export-stream.ts +++ b/apps/web/src/utils/streams/export-stream.ts @@ -99,11 +99,69 @@ export class ExportStream extends ReadableStream { const filename = sanitizeFilename(note.title, { replacement: "-" }); const ext = FORMAT_TO_EXT[format]; - controller.enqueue({ - path: makeUniqueFilename([filename, ext].join("."), counters), - data: textEncoder.encode(exported), - mtime: new Date(note.data.dateEdited), - ctime: new Date(note.data.dateCreated) + const notebooksWithoutTopics = db.relations?.to( + { id: note.id, type: "note" }, + "notebook" + ); + const notebooksWithTopics = note?.notebooks; + const notebooks: Array<{ title: string; topics: Array }> = []; + + notebooksWithoutTopics?.forEach((notebook) => { + notebooks.push({ title: notebook.title, topics: [] }); + }); + + notebooksWithTopics?.forEach((notebook: any) => { + const _topics = db.notebooks?.notebook(notebook.id).topics.all; + const topics: Array = []; + + notebook?.topics?.forEach((topicId: string) => { + _topics?.forEach((topic) => { + if (topic.id === topicId) { + topics.push(topic.title); + } + }); + }); + + notebooks.push({ + title: db.notebooks?.notebook(notebook.id).title, + topics: topics + }); + }); + + const paths: Array = []; + if (notebooks?.length > 0) { + notebooks.forEach((notebook) => { + if (notebook.topics.length > 0) + notebook.topics.forEach((topic) => { + paths.push( + makeUniqueFilename( + [filename, ext].join("."), + counters, + notebook.title, + topic + ) + ); + }); + else + paths.push( + makeUniqueFilename( + [filename, ext].join("."), + counters, + notebook.title + ) + ); + }); + } else { + paths.push(makeUniqueFilename([filename, ext].join("."), counters)); + } + + paths.forEach((path) => { + controller.enqueue({ + path, + data: textEncoder.encode(exported), + mtime: new Date(note.data.dateEdited), + ctime: new Date(note.data.dateCreated) + }); }); if (index === noteIds.length) { diff --git a/apps/web/src/utils/streams/utils.ts b/apps/web/src/utils/streams/utils.ts index 71f95ce17..110fc3b81 100644 --- a/apps/web/src/utils/streams/utils.ts +++ b/apps/web/src/utils/streams/utils.ts @@ -19,9 +19,15 @@ along with this program. If not, see . export function makeUniqueFilename( filePath: string, - counters: Record + counters: Record, + notebook?: string, + topic?: string ) { - filePath = filePath.toLowerCase(); + if (notebook && topic) + filePath = `/${notebook}/${topic}/${filePath.toLowerCase()}`; + else if (notebook) filePath = `/${notebook}/${filePath.toLowerCase()}`; + else filePath = filePath.toLowerCase(); + counters[filePath] = (counters[filePath] || 0) + 1; if (counters[filePath] === 1) return filePath;