diff --git a/src/functions/folderNoteFunctions.ts b/src/functions/folderNoteFunctions.ts index b298e7d..c6d2d63 100644 --- a/src/functions/folderNoteFunctions.ts +++ b/src/functions/folderNoteFunctions.ts @@ -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}`; diff --git a/src/settings/GeneralSettings.ts b/src/settings/GeneralSettings.ts index 8bc6100..098c33f 100644 --- a/src/settings/GeneralSettings.ts +++ b/src/settings/GeneralSettings.ts @@ -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(); }) diff --git a/src/settings/SettingsTab.ts b/src/settings/SettingsTab.ts index 91e3c57..d684cb8 100644 --- a/src/settings/SettingsTab.ts +++ b/src/settings/SettingsTab.ts @@ -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');