#156 - improved support for index-note-based folder notes: PUT ON HOLD

- turned out to be not so trivial with one challenging place
- PUT ON HOLD
- added inline annotations with !!! (intentionally breaking the TS syntax)
This commit is contained in:
SebastianMC 2024-09-01 17:07:56 +02:00
parent e66d7be9d3
commit 605d5026a6
3 changed files with 51 additions and 12 deletions

View File

@ -466,12 +466,18 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus
case CustomSortGroupType.HasMetadataField: case CustomSortGroupType.HasMetadataField:
if (group.withMetadataFieldName) { if (group.withMetadataFieldName) {
if (ctx?._mCache) { 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 notePathToScan: string = aFile ? entry.path : `${entry.path}/${entry.name}.md`
const frontMatterCache: FrontMatterCache | undefined = ctx._mCache.getCache(notePathToScan)?.frontmatter const frontMatterCache: FrontMatterCache | undefined = ctx._mCache.getCache(notePathToScan)?.frontmatter
const hasMetadata: boolean | undefined = frontMatterCache?.hasOwnProperty(group.withMetadataFieldName) 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 determined = true
} }
} }
@ -554,6 +560,12 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus
if (isPrimaryOrderByMetadata || isSecondaryOrderByMetadata || isDerivedPrimaryByMetadata || isDerivedSecondaryByMetadata) { if (isPrimaryOrderByMetadata || isSecondaryOrderByMetadata || isDerivedPrimaryByMetadata || isDerivedSecondaryByMetadata) {
if (ctx?._mCache) { if (ctx?._mCache) {
// For folders - scan metadata of 'folder note' // 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 notePathToScan: string = aFile ? entry.path : `${entry.path}/${entry.name}.md`
const frontMatterCache: FrontMatterCache | undefined = ctx._mCache.getCache(notePathToScan)?.frontmatter const frontMatterCache: FrontMatterCache | undefined = ctx._mCache.getCache(notePathToScan)?.frontmatter
if (isPrimaryOrderByMetadata) metadataValueToSortBy = frontMatterCache?.[group?.byMetadataField || group?.withMetadataFieldName || DEFAULT_METADATA_FIELD_FOR_SORTING] if (isPrimaryOrderByMetadata) metadataValueToSortBy = frontMatterCache?.[group?.byMetadataField || group?.withMetadataFieldName || DEFAULT_METADATA_FIELD_FOR_SORTING]

View File

@ -125,10 +125,14 @@ export default class CustomSortPlugin extends Plugin {
if (aFile.name === SORTSPEC_FILE_NAME || // file name == sortspec.md ? if (aFile.name === SORTSPEC_FILE_NAME || // file name == sortspec.md ?
aFile.name === `${SORTSPEC_FILE_NAME}.md` || // file name == sortspec.md.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 === parent.name || // Folder Note mode: inside folder, same name
aFile.basename === this.settings.additionalSortspecFile || // when user configured _about_ aFile.basename === this.settings.additionalSortspecFile || // when user configured _about_
aFile.name === this.settings.additionalSortspecFile || // when user configured _about_.md 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 || // 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] 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 // 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 data: any = await this.loadData() || {}
const isFreshInstall: boolean = Object.keys(data).length === 0 const isFreshInstall: boolean = Object.keys(data).length === 0
this.settings = Object.assign({}, DEFAULT_SETTINGS, data); 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) this.settings = Object.assign(this.settings, DEFAULT_SETTING_FOR_1_2_0_UP)
} }
} }

View File

@ -4,6 +4,7 @@ import CustomSortPlugin from "./main";
export interface CustomSortPluginSettings { export interface CustomSortPluginSettings {
additionalSortspecFile: string additionalSortspecFile: string
indexNoteNameForFolderNotes: string
suspended: boolean suspended: boolean
statusBarEntryEnabled: boolean statusBarEntryEnabled: boolean
notificationsEnabled: boolean notificationsEnabled: boolean
@ -16,6 +17,7 @@ export interface CustomSortPluginSettings {
export const DEFAULT_SETTINGS: CustomSortPluginSettings = { export const DEFAULT_SETTINGS: CustomSortPluginSettings = {
additionalSortspecFile: '', additionalSortspecFile: '',
indexNoteNameForFolderNotes: '',
suspended: true, // if false by default, it would be hard to handle the auto-parse after plugin install suspended: true, // if false by default, it would be hard to handle the auto-parse after plugin install
statusBarEntryEnabled: true, statusBarEntryEnabled: true,
notificationsEnabled: true, notificationsEnabled: true,
@ -52,16 +54,10 @@ export class CustomSortSettingTab extends PluginSettingTab {
// containerEl.createEl('h2', {text: 'Settings for Custom File Explorer Sorting Plugin'}); // containerEl.createEl('h2', {text: 'Settings for Custom File Explorer Sorting Plugin'});
const additionalSortspecFileDescr: DocumentFragment = sanitizeHTMLToDom( 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<sup><b>*</b></sup>.' 'A note name or note path to scan (YAML frontmatter) for sorting specification in addition to the `sortspec` notes and Folder Notes.'
+ '<br>' + '<br>'
+ ' The `.md` filename suffix is optional.' + ' The `.md` filename suffix is optional.'
+ '<p><b>(*)</b>&nbsp;if you employ the <i>Index-File based</i> approach to folder notes (as documented in '
+ '<a href="https://github.com/aidenlx/alx-folder-note/wiki/folder-note-pref"'
+ '>Aidenlx Folder Note preferences</a>'
+ ') you can enter here the index note name, e.g. <b>_about_</b>'
+ '<br>' + '<br>'
+ 'The <i>Inside Folder, with Same Name Recommended</i> mode of Folder Notes is handled automatically, no additional configuration needed.'
+ '</p>'
+ '<p>NOTE: After updating this setting remember to refresh the custom sorting via clicking on the ribbon icon or via the <b>sort-on</b> command' + '<p>NOTE: After updating this setting remember to refresh the custom sorting via clicking on the ribbon icon or via the <b>sort-on</b> command'
+ ' or by restarting Obsidian or reloading the vault</p>' + ' or by restarting Obsidian or reloading the vault</p>'
) )
@ -70,13 +66,40 @@ export class CustomSortSettingTab extends PluginSettingTab {
.setName('Path or name of additional note(s) containing sorting specification') .setName('Path or name of additional note(s) containing sorting specification')
.setDesc(additionalSortspecFileDescr) .setDesc(additionalSortspecFileDescr)
.addText(text => text .addText(text => text
.setPlaceholder('e.g. _about_') .setPlaceholder('e.g. sorting-configuration')
.setValue(this.plugin.settings.additionalSortspecFile) .setValue(this.plugin.settings.additionalSortspecFile)
.onChange(async (value) => { .onChange(async (value) => {
this.plugin.settings.additionalSortspecFile = value.trim() ? normalizePath(value) : ''; this.plugin.settings.additionalSortspecFile = value.trim() ? normalizePath(value) : '';
await this.plugin.saveSettings(); await this.plugin.saveSettings();
})); }));
const indexNoteNameDescr: DocumentFragment = sanitizeHTMLToDom(
'If you employ the <i>Index-File based</i> approach to folder notes (as documented in '
+ '<a href="https://github.com/aidenlx/alx-folder-note/wiki/folder-note-pref"'
+ '>Aidenlx Folder Note preferences</a>'
+ ') enter here the index note name, e.g. <b>_about_</b> or <b>index</b>'
+ '<br>'
+ ' The `.md` filename suffix is optional.'
+ '<br>'
+ 'This will tell the plugin to read sorting specs and also folders metadata from these files.'
+ '<br></br>'
+ 'The <i>Inside Folder, with Same Name Recommended</i> mode of Folder Notes is handled automatically, no additional configuration needed.'
+ '</p>'
+ '<p>NOTE: After updating this setting remember to refresh the custom sorting via clicking on the ribbon icon or via the <b>sort-on</b> command'
+ ' or by restarting Obsidian or reloading the vault</p>'
)
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) new Setting(containerEl)
.setName('Enable the status bar entry') .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.') .setDesc('The status bar entry shows the label `Custom sort:ON` or `Custom sort:OFF`, representing the current state of the plugin.')