Fix renaming of folder notes who are in the parent folder

This commit is contained in:
Lost Paul 2025-02-01 11:35:51 +01:00
parent e3e51062fa
commit abf6336514
3 changed files with 24 additions and 12 deletions

View file

@ -335,7 +335,7 @@ export function getFolder(plugin: FolderNotesPlugin, file: TFile, storageLocatio
let folderPath = getFolderPathFromString(file.path);
let folder: TFolder | TAbstractFile | null = null;
if ((plugin.settings.storageLocation === 'parentFolder' || storageLocation === 'parentFolder') && storageLocation !== 'insideFolder') {
if (folderPath.trim() === '') {
if (folderPath.trim() === '' || folderPath === '/') {
folderPath = folderName;
} else {
folderPath = `${folderPath}/${folderName}`;

View file

@ -32,7 +32,7 @@ export async function renderGeneral(settingsTab: SettingsTab) {
settingsTab.plugin,
'Rename all existing folder notes',
'When you click on "Confirm" all existing folder notes will be renamed to the new folder note name.',
settingsTab.updateFolderNotes,
settingsTab.renameFolderNotes,
[])
.open();
})

View file

@ -2,7 +2,7 @@ import { App, Notice, PluginSettingTab, TFile, TFolder, MarkdownPostProcessorCon
import FolderNotesPlugin from '../main';
import { ExcludePattern } from 'src/ExcludeFolders/ExcludePattern';
import { ExcludedFolder } from 'src/ExcludeFolders/ExcludeFolder';
import { getFolderNote } from '../functions/folderNoteFunctions';
import { extractFolderName, getFolderNote } from '../functions/folderNoteFunctions';
import { overviewSettings } from '../obsidian-folder-overview/src/FolderOverview';
import { renderGeneral } from './GeneralSettings';
import { renderFileExplorer } from './FileExplorerSettings';
@ -264,22 +264,34 @@ export class SettingsTab extends PluginSettingTab {
}
}
updateFolderNotes() {
renameFolderNotes() {
new Notice('Starting to update folder notes...');
console.log('starting to update folder notes 2');
const oldTemplate = this.plugin.settings.oldFolderNoteName ?? '{{folder_name}}';
for (const folder of this.app.vault.getAllLoadedFiles()) {
if (folder instanceof TFolder) {
const folderNote = getFolderNote(this.plugin, folder.path, undefined, undefined, oldTemplate);
if (!(folderNote instanceof TFile)) { continue }
const oldFolderNoteName = oldTemplate.replace('{{folder_name}}', folder.name);
const oldPath = `${folder.path}/${oldFolderNoteName}.${folderNote.extension}`;
if (folderNote.path !== oldPath) { continue }
const newFolderNoteName = this.plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name);
const newPath = `${folder.path}/${newFolderNoteName}.${folderNote.extension}`;
if (this.plugin.app.vault.getAbstractFileByPath(newPath)) { continue }
this.plugin.app.fileManager.renameFile(folderNote, newPath);
if (!(folderNote instanceof TFile)) { continue; }
const folderName = extractFolderName(oldTemplate, folderNote.basename) ?? '';
const newFolderNoteName = this.plugin.settings.folderNoteName.replace('{{folder_name}}', folderName);
let newPath = '';
if (this.plugin.settings.storageLocation === 'parentFolder') {
if (getFolderPathFromString(folder.path).trim() === '/') {
newPath = `${newFolderNoteName}.${folderNote.extension}`;
} else {
newPath = `${folderNote.parent?.path}/${newFolderNoteName}.${folderNote.extension}`;
}
} else if (this.plugin.settings.storageLocation === 'insideFolder') {
newPath = `${folder.path}/${newFolderNoteName}.${folderNote.extension}`;
}
this.app.fileManager.renameFile(folderNote, newPath);
}
}
this.plugin.settings.oldFolderNoteName = this.plugin.settings.folderNoteName;
this.plugin.saveSettings();
new Notice('Finished updating folder notes');