From 83444909066ae0d5a4da35e8e9e8e2d0e1608142 Mon Sep 17 00:00:00 2001 From: Paul <70213368+LostPaul@users.noreply.github.com> Date: Tue, 10 Mar 2026 15:49:50 +0100 Subject: [PATCH] Hide duplicated folder note name in path --- src/events/MutationObserver.ts | 34 +++++++++++++++-- src/functions/utils.ts | 4 +- src/main.ts | 69 +++++++++++++++++++--------------- src/settings/PathSettings.ts | 13 +++++++ src/settings/SettingsTab.ts | 2 + styles.css | 4 ++ 6 files changed, 90 insertions(+), 36 deletions(-) diff --git a/src/events/MutationObserver.ts b/src/events/MutationObserver.ts index 1a6a01d..aadd237 100644 --- a/src/events/MutationObserver.ts +++ b/src/events/MutationObserver.ts @@ -1,6 +1,6 @@ import { Keymap, Platform } from 'obsidian'; import type FolderNotesPlugin from 'src/main'; -import { getFolderNote } from 'src/functions/folderNoteFunctions'; +import { getFolderNote, getFolderNoteFolder } from 'src/functions/folderNoteFunctions'; import { handleViewHeaderClick } from './handleClick'; import { getExcludedFolder } from 'src/ExcludeFolders/functions/folderFunctions'; import { updateCSSClassesForFolder } from 'src/functions/styleFunctions'; @@ -162,10 +162,23 @@ async function updateFolderNamesInPath( plugin: FolderNotesPlugin, titleContainer: HTMLElement, ): Promise { - const headers = titleContainer.querySelectorAll('span.view-header-breadcrumb'); + const titleParent = titleContainer.querySelector('.view-header-title-parent'); let path = ''; const TRAILING_SLASH_LENGTH = 1; - headers.forEach(async (breadcrumb: HTMLElement) => { + + if (titleParent?.childNodes.length === 0) { + titleContainer.classList.remove('hide-folder-note-title-in-path'); + } + // eslint-disable-next-line complexity + titleParent?.childNodes.forEach(async (breadcrumb: HTMLElement) => { + if (!(breadcrumb instanceof HTMLElement)) return; + if (breadcrumb.classList.contains('view-header-breadcrumb-separator')) { + if (breadcrumb.nextSibling === null) { + breadcrumb.classList.add('is-last-separator'); + } + return; + } + path += breadcrumb.getAttribute('old-name') ?? (breadcrumb as HTMLElement).innerText.trim(); path += '/'; const folderPath = path.slice(0, -TRAILING_SLASH_LENGTH); @@ -173,6 +186,21 @@ async function updateFolderNamesInPath( const excludedFolder = getExcludedFolder(plugin, folderPath, true); if (excludedFolder?.disableFolderNote) return; const folderNote = getFolderNote(plugin, folderPath); + const viewHeaderTitle = titleContainer.querySelector('.view-header-title'); + + if (viewHeaderTitle) { + const filePath = path + (viewHeaderTitle as HTMLElement).innerText.trim() + '.md'; + const file = plugin.app.vault.getAbstractFileByPath( + filePath); + const folder = getFolderNoteFolder(plugin, file?.path ?? '', file?.name ?? ''); + if (folder && file && file.path === folderNote?.path && file.parent?.path !== '/') { + viewHeaderTitle.parentElement?.classList.add('hide-folder-note-title-in-path'); + viewHeaderTitle.classList.add('path-is-folder-note'); + } else { + viewHeaderTitle.parentElement?.classList.remove('hide-folder-note-title-in-path'); + viewHeaderTitle.classList.remove('path-is-folder-note'); + } + } if (!folderNote) return; if (folderNote) breadcrumb.classList.add('has-folder-note'); breadcrumb?.setAttribute('data-path', path.slice(0, -TRAILING_SLASH_LENGTH)); diff --git a/src/functions/utils.ts b/src/functions/utils.ts index be765f2..4d97294 100644 --- a/src/functions/utils.ts +++ b/src/functions/utils.ts @@ -49,9 +49,9 @@ export function getFileExplorer( if (!leaf) { return undefined; } /* make.md plugin integration */ - if ((leaf.containerEl?.lastChild as HTMLElement)?.dataset?.type == 'mk-path-view') { + if ((leaf.containerEl?.lastChild as HTMLElement)?.dataset?.type === 'mk-path-view') { plugin.app.workspace.iterateAllLeaves((x) => { - if ((x as FileExplorerWorkspaceLeaf).tabHeaderEl?.dataset?.type == 'file-explorer') { + if ((x as FileExplorerWorkspaceLeaf).tabHeaderEl?.dataset?.type === 'file-explorer') { leaf = x as FileExplorerWorkspaceLeaf; } }); diff --git a/src/main.ts b/src/main.ts index 493726c..799be26 100644 --- a/src/main.ts +++ b/src/main.ts @@ -59,37 +59,7 @@ export default class FolderNotesPlugin extends Plugin { this.fvIndexDB = new FvIndexDB(this); // Add CSS Classes - document.body.classList.add('folder-notes-plugin'); - if (this.settings.hideFolderNote) { document.body.classList.add('hide-folder-note'); } - if (this.settings.hideCollapsingIconForEmptyFolders) { - document.body.classList.add('fn-hide-empty-collapse-icon'); - } - if (this.settings.underlineFolder) { document.body.classList.add('folder-note-underline'); } - if (this.settings.boldName) { document.body.classList.add('folder-note-bold'); } - if (this.settings.cursiveName) { document.body.classList.add('folder-note-cursive'); } - if (this.settings.boldNameInPath) { document.body.classList.add('folder-note-bold-path'); } - if (this.settings.cursiveNameInPath) { - document.body.classList.add('folder-note-cursive-path'); - } - if (this.settings.underlineFolderInPath) { - document.body.classList.add('folder-note-underline-path'); - } - if (this.settings.stopWhitespaceCollapsing) { - document.body.classList.add('fn-whitespace-stop-collapsing'); - } - if (this.settings.hideCollapsingIcon) { - document.body.classList.add('fn-hide-collapse-icon'); - } - if (this.settings.ignoreAttachmentFolder) { - document.body.classList.add('fn-ignore-attachment-folder'); - } - if (!this.settings.highlightFolder) { - document.body.classList.add('disable-folder-highlight'); - } - - if (requireApiVersion('1.7.2')) { - document.body.classList.add('version-1-7-2'); - } + this.addSettingCssClasses(); new Commands(this.app, this).registerCommands(); registerOverviewCommands(this); @@ -161,6 +131,43 @@ export default class FolderNotesPlugin extends Plugin { ); } + addSettingCssClasses(): void { + document.body.classList.add('folder-notes-plugin'); + if (this.settings.hideFolderNote) { document.body.classList.add('hide-folder-note'); } + if (this.settings.hideCollapsingIconForEmptyFolders) { + document.body.classList.add('fn-hide-empty-collapse-icon'); + } + if (this.settings.hideFolderNoteNameInPath) { + document.body.classList.add('folder-note-hide-name-path'); + } + if (this.settings.underlineFolder) { document.body.classList.add('folder-note-underline'); } + if (this.settings.boldName) { document.body.classList.add('folder-note-bold'); } + if (this.settings.cursiveName) { document.body.classList.add('folder-note-cursive'); } + if (this.settings.boldNameInPath) { document.body.classList.add('folder-note-bold-path'); } + if (this.settings.cursiveNameInPath) { + document.body.classList.add('folder-note-cursive-path'); + } + if (this.settings.underlineFolderInPath) { + document.body.classList.add('folder-note-underline-path'); + } + if (this.settings.stopWhitespaceCollapsing) { + document.body.classList.add('fn-whitespace-stop-collapsing'); + } + if (this.settings.hideCollapsingIcon) { + document.body.classList.add('fn-hide-collapse-icon'); + } + if (this.settings.ignoreAttachmentFolder) { + document.body.classList.add('fn-ignore-attachment-folder'); + } + if (!this.settings.highlightFolder) { + document.body.classList.add('disable-folder-highlight'); + } + + if (requireApiVersion('1.7.2')) { + document.body.classList.add('version-1-7-2'); + } + } + onLayoutReady(): void { if (!this._loaded) { return; diff --git a/src/settings/PathSettings.ts b/src/settings/PathSettings.ts index 034b1ed..3e7494c 100644 --- a/src/settings/PathSettings.ts +++ b/src/settings/PathSettings.ts @@ -113,4 +113,17 @@ export async function renderPath(settingsTab: SettingsTab): Promise { await settingsTab.plugin.saveSettings(); }), ); + + new Setting(containerEl) + .setName('Hide folder note name in the path') + .setDesc('Only show the folder name in the path and hide the folder note name.') + .addToggle((toggle) => + toggle + .setValue(settingsTab.plugin.settings.hideFolderNoteNameInPath) + .onChange(async (value) => { + document.body.classList.toggle('folder-note-hide-name-path', value); + settingsTab.plugin.settings.hideFolderNoteNameInPath = value; + await settingsTab.plugin.saveSettings(); + }), + ); } diff --git a/src/settings/SettingsTab.ts b/src/settings/SettingsTab.ts index 3139136..80c98d0 100644 --- a/src/settings/SettingsTab.ts +++ b/src/settings/SettingsTab.ts @@ -82,6 +82,7 @@ export interface FolderNotesSettings { fvGlobalSettings: { autoUpdateLinks: boolean; } + hideFolderNoteNameInPath: boolean; } export const DEFAULT_SETTINGS: FolderNotesSettings = { @@ -206,6 +207,7 @@ export const DEFAULT_SETTINGS: FolderNotesSettings = { fvGlobalSettings: { autoUpdateLinks: false, }, + hideFolderNoteNameInPath: false, }; export class SettingsTab extends PluginSettingTab { diff --git a/styles.css b/styles.css index 1466d0d..f9f2c11 100644 --- a/styles.css +++ b/styles.css @@ -222,6 +222,10 @@ li:has(.fv-link-list-item), .folder-note-cursive-path .has-folder-note.view-header-breadcrumb { font-style: italic; } +.folder-note-hide-name-path .hide-folder-note-title-in-path .is-last-separator, +.folder-note-hide-name-path .hide-folder-note-title-in-path .path-is-folder-note { + display: none; +} /* Collapse Icon Handling */