Skip to content

Commit b2d4145

Browse files
committed
Prototyping the support for {{parent-folder-name}} in settings of index note name.
- need to review with a fresh head - unit tests - are the affected areas covered already? - testing live needed, especially from the perspective of root folder handling
1 parent 284bfdf commit b2d4145

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

src/custom-sort/custom-sort.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
RegExpSpec
2222
} from "./custom-sort-types";
2323
import {
24-
isDefined
24+
isDefined, resolveIndexFileName
2525
} from "../utils/utils";
2626
import {
2727
expandMacros
@@ -480,7 +480,8 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus
480480
if (aFolder) {
481481
const indexNoteBasename = ctx?.plugin?.indexNoteBasename()
482482
if (indexNoteBasename) {
483-
frontMatterCache = ctx._mCache.getCache(`${entry.path}/${indexNoteBasename}.md`)?.frontmatter
483+
let resolvedIndexNoteBasename = resolveIndexFileName(indexNoteBasename, entry.parent?.name||'')
484+
frontMatterCache = ctx._mCache.getCache(`${entry.path}/${resolvedIndexNoteBasename}.md`)?.frontmatter
484485
hasMetadata = hasMetadata || frontMatterCache?.hasOwnProperty(group.withMetadataFieldName)
485486
}
486487
}
@@ -567,7 +568,8 @@ export const determineSortingGroup = function (entry: TFile | TFolder, spec: Cus
567568
if (aFolder) {
568569
const indexNoteBasename = ctx?.plugin?.indexNoteBasename()
569570
if (indexNoteBasename) {
570-
prioFrontMatterCache = ctx._mCache.getCache(`${entry.path}/${indexNoteBasename}.md`)?.frontmatter
571+
let resolvedIndexNoteBasename = resolveIndexFileName(indexNoteBasename, entry.parent?.name||'')
572+
prioFrontMatterCache = ctx._mCache.getCache(`${entry.path}/${resolvedIndexNoteBasename}.md`)?.frontmatter
571573
}
572574
}
573575
if (isPrimaryOrderByMetadata) metadataValueToSortBy =

src/main.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
} from "./utils/ObsidianIconFolderPluginSignature";
5050
import {
5151
extractBasename,
52-
lastPathComponent,
52+
lastPathComponent, resolveIndexFileName,
5353
ValueOrError,
5454
} from "./utils/utils";
5555
import {
@@ -133,6 +133,11 @@ export default class CustomSortPlugin
133133
if (file instanceof TFile) {
134134
const aFile: TFile = file as TFile
135135
const parent: TFolder = aFile.parent!
136+
let resolvedIndexNoteNameForFolderNotes = ''
137+
if (this.settings.indexNoteNameForFolderNotes) {
138+
resolvedIndexNoteNameForFolderNotes = resolveIndexFileName(this.settings.indexNoteNameForFolderNotes, parent.name)
139+
}
140+
136141
// Read sorting spec from three sources of equal priority:
137142
// - files with designated predefined name
138143
// - files with the same name as parent folders (aka folder notes), e.g.: References/References.md
@@ -148,8 +153,10 @@ export default class CustomSortPlugin
148153
aFile.path === this.settings.additionalSortspecFile || // when user configured Inbox/sort.md
149154
aFile.path === `${this.settings.additionalSortspecFile}.md` || // when user configured Inbox/sort
150155

151-
aFile.basename === this.settings.indexNoteNameForFolderNotes || // when user configured as index
152-
aFile.name === this.settings.indexNoteNameForFolderNotes // when user configured as index.md
156+
(resolvedIndexNoteNameForFolderNotes && (
157+
aFile.basename === resolvedIndexNoteNameForFolderNotes || // when user configured as index
158+
aFile.name === resolvedIndexNoteNameForFolderNotes // when user configured as index.md
159+
))
153160
) {
154161
const sortingSpecTxt: string|undefined = mCache.getCache(aFile.path)?.frontmatter?.[SORTINGSPEC_YAML_KEY]
155162
// Warning: newer Obsidian versions can return objects as well, hence the explicit check for string value

src/settings.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ export class CustomSortSettingTab extends PluginSettingTab {
111111
+ '>Aidenlx Folder Note preferences</a>'
112112
+ ') enter here the index note name, e.g. <b>_about_</b> or <b>index</b>'
113113
+ '<br>'
114+
+ ' The template {{parent-folder-name}} is supported, e.g. <b>_{{parent-folder-name}}</b>'
115+
+ '<br>'
114116
+ ' The `.md` filename suffix is optional.'
115117
+ '<br>'
116118
+ 'This will tell the plugin to read sorting specs and also folders metadata from these files.'
@@ -125,7 +127,7 @@ export class CustomSortSettingTab extends PluginSettingTab {
125127
.setName('Name of index note (Folder Notes support)')
126128
.setDesc(indexNoteNameDescr)
127129
.addText(text => text
128-
.setPlaceholder('e.g. _about_ or index')
130+
.setPlaceholder('e.g. _about_ or index or _{{parent-folder-name}}')
129131
.setValue(this.plugin.settings.indexNoteNameForFolderNotes)
130132
.onChange(async (value) => {
131133
this.plugin.settings.indexNoteNameForFolderNotes = value.trim() ? normalizePath(value) : '';

src/utils/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ export function extractBasename (configEntry: string | undefined): string | unde
2525
}
2626
}
2727

28+
export function resolveIndexFileName(indexFileName: string, parentFolderName: string): string {
29+
if (parentFolderName && parentFolderName !== '/') {
30+
return indexFileName.replace('{{parent-folder-name}}', parentFolderName)
31+
} else {
32+
return indexFileName
33+
}
34+
}
35+
2836
export class ValueOrError<V,E> {
2937
constructor(private value?: V, private error?: E) {
3038
if (value) this.error = undefined

0 commit comments

Comments
 (0)