Custom folder note name

This commit is contained in:
Lost Paul 2023-05-15 05:42:31 +02:00
parent 2a19e7afe0
commit 05de0d8c64
4 changed files with 46 additions and 16 deletions

View file

@ -34,8 +34,8 @@ export class Commands {
new Notice('Successfully excluded folder from folder notes');
});
});
const folderNote = this.plugin.app.vault.getAbstractFileByPath(file.path + '/' + file.name + '.md');
const path = file.path + '/' + this.plugin.settings.folderNoteName.replace('{{folder_name}}', file.name) + '.md';
const folderNote = this.plugin.app.vault.getAbstractFileByPath(path);
if (folderNote && folderNote instanceof TFile) {
menu.addItem((item) => {
item.setTitle('Delete folder note')
@ -49,7 +49,7 @@ export class Commands {
item.setTitle('Create folder note')
.setIcon('edit')
.onClick(() => {
this.plugin.createFolderNote(file.path + '/' + file.name + '.md', true);
this.plugin.createFolderNote(path, true);
});
});
}

View file

@ -44,7 +44,8 @@ export default class FolderNotesPlugin extends Plugin {
breadcrumbs.forEach((breadcrumb: HTMLElement) => {
path += breadcrumb.innerText.trim() + '/';
breadcrumb.setAttribute('data-path', path.slice(0, -1));
if (this.app.vault.getAbstractFileByPath(path + this.getNameFromPathString(path.slice(0, -1)) + '.md')) {
const folderNotePath = path + this.settings.folderNoteName.replace('{{folder_name}}', this.getNameFromPathString(path.slice(0, -1))) + '.md';
if (this.app.vault.getAbstractFileByPath(folderNotePath)) {
breadcrumb.classList.add('has-folder-note');
}
});
@ -137,7 +138,7 @@ export default class FolderNotesPlugin extends Plugin {
event.target.onclick = null;
event.target.click();
}
const path = folder + '/' + event.target.innerText + '.md';
const path = folder + '/' + this.settings.folderNoteName.replace('{{folder_name}}', event.target.innerText) + '.md';
if (this.app.vault.getAbstractFileByPath(path)) {
return this.openFolderNote(path, event);
@ -186,7 +187,7 @@ export default class FolderNotesPlugin extends Plugin {
handleFolderRename(file: TFolder, oldPath: string) {
const oldFileName = this.getNameFromPathString(oldPath);
const folder = this.app.vault.getAbstractFileByPath(file.path);
if (!folder) return;
if (!(folder instanceof TFolder)) return;
const excludedFolders = this.settings.excludeFolders.filter(
(excludedFolder) => excludedFolder.path.includes(oldPath)
);
@ -208,11 +209,12 @@ export default class FolderNotesPlugin extends Plugin {
const excludedFolder = this.getExcludedFolderByPath(oldPath);
if (excludedFolder?.disableSync) return;
const newPath = folder.path + '/' + folder.name + '.md';
if (!(folder instanceof TFolder)) return;
const note = this.app.vault.getAbstractFileByPath(oldPath + '/' + oldFileName + '.md');
const newPath = folder.path + '/' + this.settings.folderNoteName.replace('{{folder_name}}', folder.name) + '.md';
const folderNotePath = oldPath + '/' + this.settings.folderNoteName.replace('{{folder_name}}', oldFileName) + '.md';
const note = this.app.vault.getAbstractFileByPath(folderNotePath);
if (!note) return;
(note as TFile).path = folder.path + '/' + oldFileName + '.md';
if (!(note instanceof TFile)) return;
note.path = folder.path + '/' + this.settings.folderNoteName.replace('{{folder_name}}', oldFileName) + '.md';
this.app.vault.rename(note, newPath);
}
@ -223,7 +225,6 @@ export default class FolderNotesPlugin extends Plugin {
const newFilePath = this.getPathFromString(file.path);
const newFolder = this.app.vault.getAbstractFileByPath(newFilePath);
if (oldFolder) {
const excludedFolder = this.getExcludedFolderByPath(oldFolder.path);
if (excludedFolder?.disableSync) {
@ -242,7 +243,7 @@ export default class FolderNotesPlugin extends Plugin {
// file matched folder name before rename
// file hasnt moved just renamed
// Need to rename the folder
if (oldFolder && oldFolder.name + '.md' === oldFileName && newFilePath === oldFilePath) {
if (oldFolder && this.settings.folderNoteName.replace('{{folder_name}}', oldFolder.name) + '.md' === oldFileName && newFilePath === oldFilePath) {
return this.renameFolderOnFileRename(file, oldPath, oldFolder);
}
@ -262,7 +263,10 @@ export default class FolderNotesPlugin extends Plugin {
}
async renameFolderOnFileRename(file: TFile, oldPath: string, oldFolder: TAbstractFile) {
const newFolderPath = oldFolder.parent.path + '/' + file.basename;
const newFolderName = this.extractFolderName(this.settings.folderNoteName, file.basename);
if (!newFolderName) return;
const newFolderPath = oldFolder.parent.path + '/' + this.extractFolderName(this.settings.folderNoteName, file.basename);
if (this.app.vault.getAbstractFileByPath(newFolderPath)) {
await this.app.vault.rename(file, oldPath);
return new Notice('A folder with the same name already exists');
@ -270,6 +274,16 @@ export default class FolderNotesPlugin extends Plugin {
await this.app.vault.rename(oldFolder, newFolderPath);
}
extractFolderName(template: string, changedFileName: string) {
const [prefix, suffix] = template.split('{{folder_name}}');
if (changedFileName.startsWith(prefix)) {
return changedFileName.slice(prefix.length).replace(suffix, '');
} else if (changedFileName.endsWith(suffix)) {
return changedFileName.slice(prefix.length, -suffix.length);
}
return null;
}
handleFileCreate(file: TFile) {
if (file.basename !== file.parent.name) { return; }
this.addCSSClassToTitleEL(file.parent.path, 'has-folder-note');

View file

@ -26,10 +26,11 @@ export default class ConfirmationModal extends Modal {
if (folder instanceof TFolder) {
const excludedFolder = this.plugin.settings.excludeFolders.find(
(excludedFolder) => (excludedFolder.path === folder.path) ||
(excludedFolder.path === folder.path?.slice(0, folder?.path.lastIndexOf('/') >= 0 ? folder.path?.lastIndexOf('/') : folder.path.length)
&& excludedFolder.subFolders));
(excludedFolder.path === folder.path?.slice(0, folder?.path.lastIndexOf('/') >= 0 ? folder.path?.lastIndexOf('/') : folder.path.length)
&& excludedFolder.subFolders));
if (excludedFolder) return;
if (this.app.vault.getAbstractFileByPath(folder.path + '/' + folder.name + '.md')) return;
const path = folder.path + '/' + this.plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name) + '.md';
if (this.app.vault.getAbstractFileByPath(path)) return;
await this.plugin.createFolderNote(folder.path + '/' + folder.name + '.md', true);
}
});

View file

@ -19,6 +19,7 @@ export interface FolderNotesSettings {
underlineFolderInPath: boolean;
openFolderNoteOnClickInPath: boolean;
openInNewTab: boolean;
folderNoteName: string;
}
export const DEFAULT_SETTINGS: FolderNotesSettings = {
@ -36,6 +37,7 @@ export const DEFAULT_SETTINGS: FolderNotesSettings = {
underlineFolderInPath: true,
openFolderNoteOnClickInPath: true,
openInNewTab: false,
folderNoteName: '{{folder_name}}',
};
export class SettingsTab extends PluginSettingTab {
plugin: FolderNotesPlugin;
@ -51,6 +53,19 @@ export class SettingsTab extends PluginSettingTab {
containerEl.createEl('h2', { text: 'Folder notes settings' });
new Setting(containerEl)
.setName('Folder note name')
.setDesc('The name of the folder note')
.addText((text) =>
text
.setValue(this.plugin.settings.folderNoteName)
.onChange(async (value) => {
if (value.trim() === '') { return; }
this.plugin.settings.folderNoteName = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('Disable folder collapsing')
.setDesc('Disable the ability to collapse folders by clicking exactly on the folder name')