diff --git a/src/custom-sort/custom-sort.ts b/src/custom-sort/custom-sort.ts index 1261261..686a252 100644 --- a/src/custom-sort/custom-sort.ts +++ b/src/custom-sort/custom-sort.ts @@ -466,12 +466,18 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus case CustomSortGroupType.HasMetadataField: if (group.withMetadataFieldName) { if (ctx?._mCache) { - // For folders - scan metadata of 'folder note' + // For folders - scan metadata of 'folder note' in same-name-as-folder mode const notePathToScan: string = aFile ? entry.path : `${entry.path}/${entry.name}.md` const frontMatterCache: FrontMatterCache | undefined = ctx._mCache.getCache(notePathToScan)?.frontmatter const hasMetadata: boolean | undefined = frontMatterCache?.hasOwnProperty(group.withMetadataFieldName) + // For folders, if index-based folder note mode, scan the index file, giving it the priority + !!! Non trivial part: - need to know the folder-index name, this is hidden + !!! in plugin settings, not exposed - refactoring is a must. + if (aFolder && ctx?.plugin?.s) { - if (hasMetadata) { + } + + if (hasMetadata || folderIndexNoteHasMetadata) { determined = true } } @@ -554,6 +560,12 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus if (isPrimaryOrderByMetadata || isSecondaryOrderByMetadata || isDerivedPrimaryByMetadata || isDerivedSecondaryByMetadata) { if (ctx?._mCache) { // For folders - scan metadata of 'folder note' + // and if index-based folder note mode, scan the index file, giving it the priority + !!! Non trivial part: - need to know the folder-index-note name, this is hidden + !!! in plugin settings, not exposed - refactoring is a must. + !!! + !!! Then challenging part - how to easily rewrite the below code to handle scanning + !!! of two frontMatterCaches for folders and give the priority to folder-index-note based cache, if configured const notePathToScan: string = aFile ? entry.path : `${entry.path}/${entry.name}.md` const frontMatterCache: FrontMatterCache | undefined = ctx._mCache.getCache(notePathToScan)?.frontmatter if (isPrimaryOrderByMetadata) metadataValueToSortBy = frontMatterCache?.[group?.byMetadataField || group?.withMetadataFieldName || DEFAULT_METADATA_FIELD_FOR_SORTING] diff --git a/src/main.ts b/src/main.ts index d9d61fe..60c4fcb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -125,10 +125,14 @@ export default class CustomSortPlugin extends Plugin { if (aFile.name === SORTSPEC_FILE_NAME || // file name == sortspec.md ? aFile.name === `${SORTSPEC_FILE_NAME}.md` || // file name == sortspec.md.md ? aFile.basename === parent.name || // Folder Note mode: inside folder, same name + aFile.basename === this.settings.additionalSortspecFile || // when user configured _about_ aFile.name === this.settings.additionalSortspecFile || // when user configured _about_.md aFile.path === this.settings.additionalSortspecFile || // when user configured Inbox/sort.md - aFile.path === `${this.settings.additionalSortspecFile}.md` // when user configured Inbox/sort + aFile.path === `${this.settings.additionalSortspecFile}.md` || // when user configured Inbox/sort + + aFile.basename === this.settings.indexNoteNameForFolderNotes || // when user configured as index + aFile.name === this.settings.indexNoteNameForFolderNotes // when user configured as index.md ) { const sortingSpecTxt: string = mCache.getCache(aFile.path)?.frontmatter?.[SORTINGSPEC_YAML_KEY] // Warning: newer Obsidian versions can return objects as well, hence the explicit check for string value @@ -713,7 +717,7 @@ export default class CustomSortPlugin extends Plugin { const data: any = await this.loadData() || {} const isFreshInstall: boolean = Object.keys(data).length === 0 this.settings = Object.assign({}, DEFAULT_SETTINGS, data); - if (requireApiVersion('1.2.0')) { + if (requireApiVersion('1.2.0') && isFreshInstall) { this.settings = Object.assign(this.settings, DEFAULT_SETTING_FOR_1_2_0_UP) } } diff --git a/src/settings.ts b/src/settings.ts index de165b0..3a0d251 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -4,6 +4,7 @@ import CustomSortPlugin from "./main"; export interface CustomSortPluginSettings { additionalSortspecFile: string + indexNoteNameForFolderNotes: string suspended: boolean statusBarEntryEnabled: boolean notificationsEnabled: boolean @@ -16,6 +17,7 @@ export interface CustomSortPluginSettings { export const DEFAULT_SETTINGS: CustomSortPluginSettings = { additionalSortspecFile: '', + indexNoteNameForFolderNotes: '', suspended: true, // if false by default, it would be hard to handle the auto-parse after plugin install statusBarEntryEnabled: true, notificationsEnabled: true, @@ -52,16 +54,10 @@ export class CustomSortSettingTab extends PluginSettingTab { // containerEl.createEl('h2', {text: 'Settings for Custom File Explorer Sorting Plugin'}); const additionalSortspecFileDescr: DocumentFragment = sanitizeHTMLToDom( - 'A note name or note path to scan (YAML frontmatter) for sorting specification in addition to the `sortspec` notes and Folder Notes*.' + 'A note name or note path to scan (YAML frontmatter) for sorting specification in addition to the `sortspec` notes and Folder Notes.' + '
' + ' The `.md` filename suffix is optional.' - + '

(*) if you employ the Index-File based approach to folder notes (as documented in ' - + 'Aidenlx Folder Note preferences' - + ') you can enter here the index note name, e.g. _about_' + '
' - + 'The Inside Folder, with Same Name Recommended mode of Folder Notes is handled automatically, no additional configuration needed.' - + '

' + '

NOTE: After updating this setting remember to refresh the custom sorting via clicking on the ribbon icon or via the sort-on command' + ' or by restarting Obsidian or reloading the vault

' ) @@ -70,13 +66,40 @@ export class CustomSortSettingTab extends PluginSettingTab { .setName('Path or name of additional note(s) containing sorting specification') .setDesc(additionalSortspecFileDescr) .addText(text => text - .setPlaceholder('e.g. _about_') + .setPlaceholder('e.g. sorting-configuration') .setValue(this.plugin.settings.additionalSortspecFile) .onChange(async (value) => { this.plugin.settings.additionalSortspecFile = value.trim() ? normalizePath(value) : ''; await this.plugin.saveSettings(); })); + const indexNoteNameDescr: DocumentFragment = sanitizeHTMLToDom( + 'If you employ the Index-File based approach to folder notes (as documented in ' + + 'Aidenlx Folder Note preferences' + + ') enter here the index note name, e.g. _about_ or index' + + '
' + + ' The `.md` filename suffix is optional.' + + '
' + + 'This will tell the plugin to read sorting specs and also folders metadata from these files.' + + '

' + + 'The Inside Folder, with Same Name Recommended mode of Folder Notes is handled automatically, no additional configuration needed.' + + '

' + + '

NOTE: After updating this setting remember to refresh the custom sorting via clicking on the ribbon icon or via the sort-on command' + + ' or by restarting Obsidian or reloading the vault

' + ) + + new Setting(containerEl) + .setName('Name of index note (Folder Notes support)') + .setDesc(indexNoteNameDescr) + .addText(text => text + .setPlaceholder('e.g. _about_ or index') + .setValue(this.plugin.settings.indexNoteNameForFolderNotes) + .onChange(async (value) => { + this.plugin.settings.indexNoteNameForFolderNotes = value.trim() ? normalizePath(value) : ''; + await this.plugin.saveSettings(); + })); + new Setting(containerEl) .setName('Enable the status bar entry') .setDesc('The status bar entry shows the label `Custom sort:ON` or `Custom sort:OFF`, representing the current state of the plugin.')