Fix #35 & other issue with renaming all folder notes

This commit is contained in:
Lost Paul 2023-11-04 21:53:30 +01:00
parent 50e515e4dd
commit 628e9392da
4 changed files with 20 additions and 30 deletions

View file

@ -1,7 +1,7 @@
import FolderNotesPlugin from 'src/main';
import { getDefer, Listener, Events, ApiInterface, DeferInterface, ListenerRef, EventDispatcherInterface } from 'front-matter-plugin-api-provider';
import { App, TFile, TFolder } from 'obsidian';
import { extractFolderName, getFolder } from 'src/functions/folderNoteFunctions';
import { extractFolderName, getFolder, getFolderNote } from 'src/functions/folderNoteFunctions';
export class FrontMatterTitlePluginHandler {
plugin: FolderNotesPlugin;
app: App;
@ -64,9 +64,9 @@ export class FrontMatterTitlePluginHandler {
const resolver = this.api?.getResolverFactory()?.createResolver('#feature-id#');
const newName = resolver?.resolve(file?.path ?? '')
const folder = getFolder(this.plugin, file);
if ((extractFolderName(this.plugin.settings.folderNoteName, file.basename) || file.basename) !== folder?.name) { return; }
if (!(folder instanceof TFolder)) { return; }
const folderNote = getFolderNote(this.plugin, folder.path);
if (!folderNote) { return }
if (isEvent) {
this.plugin.changeName(folder, newName, true);
} else {

View file

@ -222,7 +222,10 @@ export function getFolderNote(plugin: FolderNotesPlugin, folderPath: string, sto
export function getFolder(plugin: FolderNotesPlugin, file: TFile, storageLocation?: string) {
if (!file) return null;
const folderName = extractFolderName(plugin.settings.folderNoteName, file.basename);
let folderName = extractFolderName(plugin.settings.folderNoteName, file.basename);
if (plugin.settings.folderNoteName === file.basename && plugin.settings.storageLocation === 'insideFolder') {
folderName = file.parent?.name ?? '';
}
if (!folderName) return null;
let folderPath = plugin.getFolderPathFromString(file.path);
let folder: TFolder | TAbstractFile | null = null;

View file

@ -26,7 +26,7 @@ export async function renderGeneral(settingsTab: SettingsTab) {
.setButtonText('Rename existing folder notes')
.setCta()
.onClick(async () => {
settingsTab.updateFolderNotes(settingsTab.plugin.settings.folderNoteName, settingsTab.plugin.settings.newFolderNoteName);
settingsTab.updateFolderNotes(settingsTab.plugin.settings.newFolderNoteName);
})
);
nameSetting.infoEl.appendText('Make sure to back up your vault before renaming all folder notes and restart Obsidian after renaming them');

View file

@ -178,33 +178,20 @@ export class SettingsTab extends PluginSettingTab {
this.renderSettingsPage(this.plugin.settings.settingsTab);
}
updateFolderNotes(oldTemplate: string, newTemplate: string) {
updateFolderNotes(newTemplate: string) {
new Notice('Starting to update folder notes...');
for (const folder of this.app.vault.getAllLoadedFiles()) {
if (folder instanceof TFolder) {
const folderNote = getFolderNote(this.plugin, folder.path);
if (!(folderNote instanceof TFile)) { continue }
const folderNoteName = newTemplate.replace('{{folder_name}}', folder.name)
const newPath = `${folder.path}/${folderNoteName}.${folderNote.extension}`;
if (this.plugin.app.vault.getAbstractFileByPath(newPath)) { continue }
this.app.vault.rename(folderNote, newPath);
}
}
this.plugin.settings.folderNoteName = newTemplate;
this.plugin.saveSettings();
new Notice('Starting to update folder notes...');
this.app.vault.getFiles().forEach((file) => {
if (file instanceof TFile) {
const folderPath = this.plugin.getFolderPathFromString(file.path);
let folder = this.app.vault.getAbstractFileByPath(folderPath);
let folderName = file.name.slice(0, -file.extension.length - 1);
folderName = extractFolderName(oldTemplate, folderName) || '';
if (!(folder instanceof TFolder) || folderName !== folder?.name) {
if (folderPath.trim() === '') {
folder = this.app.vault.getAbstractFileByPath(folderName);
} else {
folder = this.app.vault.getAbstractFileByPath(folderPath + '/' + folderName);
}
}
if (!(folder instanceof TFolder)) { return; }
if (folderName === folder?.name) {
const newPath = `${folder?.path}/${this.plugin.settings.folderNoteName.replace('{{folder_name}}', folderName)}.${file.extension}`;
this.app.vault.rename(file, newPath);
} else if (folder?.name === file.name.slice(0, -file.extension.length - 1) || '') {
const newPath = `${folder?.path}/${this.plugin.settings.folderNoteName.replace('{{folder_name}}', file.name.slice(0, -file.extension.length - 1) || '')}.${file.extension}`;
this.app.vault.rename(file, newPath);
}
}
});
new Notice('Finished updating folder notes');
}