Skip to content

Commit

Permalink
Delete only if has own attachment folder
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaoumov committed Nov 3, 2024
1 parent f405aac commit 5b7e044
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/obsidian/AttachmentPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { parentFolderPath } from 'obsidian-typings/implementations';

import {
basename,
extname
dirname,
extname,
join
} from '../Path.ts';
import {
normalize,
Expand Down Expand Up @@ -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<boolean> {
const attachmentFolderPath = await getAttachmentFolderPath(app, path);
const dummyAttachmentFolderPath = await getAttachmentFolderPath(app, join(dirname(path), 'DUMMY_FILE.md'));
return attachmentFolderPath !== dummyAttachmentFolderPath;
}
12 changes: 9 additions & 3 deletions src/obsidian/RenameDeleteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -337,6 +340,10 @@ async function handleDelete(app: App, path: string): Promise<void> {
return;
}

if (!(await hasOwnAttachmentFolder(app, path))) {
return;
}

await deleteSafe(app, attachmentFolder, path, false, settings.shouldDeleteEmptyFolders);
}

Expand All @@ -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);

Expand All @@ -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;
Expand Down

0 comments on commit 5b7e044

Please sign in to comment.