From 5b7e0445e4152e0ce75794e2d8f65bb258b2c189 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Sat, 2 Nov 2024 23:39:20 -0600 Subject: [PATCH] Delete only if has own attachment folder --- src/obsidian/AttachmentPath.ts | 17 ++++++++++++++++- src/obsidian/RenameDeleteHandler.ts | 12 +++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/obsidian/AttachmentPath.ts b/src/obsidian/AttachmentPath.ts index 7c640b7..c478f19 100644 --- a/src/obsidian/AttachmentPath.ts +++ b/src/obsidian/AttachmentPath.ts @@ -11,7 +11,9 @@ import { parentFolderPath } from 'obsidian-typings/implementations'; import { basename, - extname + dirname, + extname, + join } from '../Path.ts'; import { normalize, @@ -126,3 +128,16 @@ function normalizeSlashes(path: string): string { path = path.replace(/(^\/+|\/+$)/g, ''); return path || '/'; } + +/** + * Checks if a note has its own attachment folder. + * + * @param app - The Obsidian application instance. + * @param path - The path of the note. + * @returns A promise that resolves to a boolean indicating whether the note has its own attachment folder. + */ +export async function hasOwnAttachmentFolder(app: App, path: string): Promise { + const attachmentFolderPath = await getAttachmentFolderPath(app, path); + const dummyAttachmentFolderPath = await getAttachmentFolderPath(app, join(dirname(path), 'DUMMY_FILE.md')); + return attachmentFolderPath !== dummyAttachmentFolderPath; +} diff --git a/src/obsidian/RenameDeleteHandler.ts b/src/obsidian/RenameDeleteHandler.ts index 6659f07..c9b2b69 100644 --- a/src/obsidian/RenameDeleteHandler.ts +++ b/src/obsidian/RenameDeleteHandler.ts @@ -25,7 +25,10 @@ import { relative } from '../Path.ts'; import { getObsidianDevUtilsState } from './App.ts'; -import { getAttachmentFolderPath } from './AttachmentPath.ts'; +import { + getAttachmentFolderPath, + hasOwnAttachmentFolder +} from './AttachmentPath.ts'; import { chain } from './ChainedPromise.ts'; import { getFile, @@ -337,6 +340,10 @@ async function handleDelete(app: App, path: string): Promise { return; } + if (!(await hasOwnAttachmentFolder(app, path))) { + return; + } + await deleteSafe(app, attachmentFolder, path, false, settings.shouldDeleteEmptyFolders); } @@ -353,7 +360,6 @@ async function fillRenameMap(app: App, oldPath: string, newPath: string, renameM const newAttachmentFolderPath = settings.shouldRenameAttachmentFolder ? await getAttachmentFolderPath(app, newPath) : oldAttachmentFolderPath; - const dummyOldAttachmentFolderPath = await getAttachmentFolderPath(app, join(dirname(oldPath), 'DUMMY_FILE.md')); const oldAttachmentFolder = getFolderOrNull(app, oldAttachmentFolderPath); @@ -367,7 +373,7 @@ async function fillRenameMap(app: App, oldPath: string, newPath: string, renameM const oldAttachmentFiles: TFile[] = []; - if (oldAttachmentFolderPath === dummyOldAttachmentFolderPath) { + if (!(await hasOwnAttachmentFolder(app, oldPath))) { const oldCache = await getCacheSafe(app, oldPath); if (!oldCache) { return;