Skip to content

Commit feed16c

Browse files
authored
Merge pull request #59 from SebastianMC/58-some-target-folder-ignored-if-multi-note-specs-present
#58 - Some target-folder: get ignored when sorting specs are read fro…
2 parents 5775ddb + f444614 commit feed16c

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

src/custom-sort/sorting-spec-processor.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ describe('SortingSpecProcessor target-folder by name and regex', () => {
930930
it('should correctly handle the by-name only target-folder', () => {
931931
const inputTxtArr: Array<string> = txtInputTargetFolderByName.split('\n')
932932
const result = processor.parseSortSpecFromText(inputTxtArr, 'mock-folder', 'custom-name-note.md')
933-
expect(result?.sortSpecByPath).toEqual({})
933+
expect(result?.sortSpecByPath).toBeUndefined()
934934
expect(result?.sortSpecByName).toEqual(expectedSortSpecsTargetFolderByName)
935935
expect(result?.sortSpecByWildcard).not.toBeNull()
936936
})

src/custom-sort/sorting-spec-processor.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -528,16 +528,33 @@ export interface FolderNameToSortSpecMap {
528528
}
529529

530530
export interface SortSpecsCollection {
531-
sortSpecByPath: FolderPathToSortSpecMap
532-
sortSpecByName: FolderNameToSortSpecMap
531+
sortSpecByPath?: FolderPathToSortSpecMap
532+
sortSpecByName?: FolderNameToSortSpecMap
533533
sortSpecByWildcard?: FolderWildcardMatching<CustomSortSpec>
534534
}
535535

536-
export const newSortSpecsCollection = (): SortSpecsCollection => {
537-
return {
538-
sortSpecByPath: {},
539-
sortSpecByName: {}
536+
const ensureCollectionHasSortSpecByPath = (collection?: SortSpecsCollection | null) => {
537+
collection = collection ?? {}
538+
if (!collection.sortSpecByPath) {
539+
collection.sortSpecByPath = {}
540+
}
541+
return collection
542+
}
543+
544+
const ensureCollectionHasSortSpecByName = (collection?: SortSpecsCollection | null) => {
545+
collection = collection ?? {}
546+
if (!collection.sortSpecByName) {
547+
collection.sortSpecByName = {}
540548
}
549+
return collection
550+
}
551+
552+
const ensureCollectionHasSortSpecByWildcard = (collection?: SortSpecsCollection | null) => {
553+
collection = collection ?? {}
554+
if (!collection.sortSpecByWildcard) {
555+
collection.sortSpecByWildcard = new FolderWildcardMatching<CustomSortSpec>()
556+
}
557+
return collection
541558
}
542559

543560
interface AdjacencyInfo {
@@ -744,7 +761,6 @@ export class SortingSpecProcessor {
744761
}
745762
}
746763

747-
let sortspecByName: FolderNameToSortSpecMap | undefined
748764
for (let spec of this.ctx.specs) {
749765
// Consume the folder names prefixed by the designated lexeme
750766
for (let idx = 0; idx<spec.targetFoldersPaths.length; idx++) {
@@ -756,42 +772,36 @@ export class SortingSpecProcessor {
756772
`Empty '${TargetFolderLexeme} ${MatchFolderNameLexeme}' value` )
757773
return null // Failure - not allow duplicate by folderNameToMatch specs for the same folder folderNameToMatch
758774
}
759-
sortspecByName = sortspecByName ?? {}
760-
if (sortspecByName[folderNameToMatch]) {
775+
collection = ensureCollectionHasSortSpecByName(collection)
776+
if (collection.sortSpecByName![folderNameToMatch]) {
761777
this.problem(ProblemCode.DuplicateByNameSortSpecForFolder,
762778
`Duplicate '${TargetFolderLexeme} ${MatchFolderNameLexeme}' definition for the same name <${folderNameToMatch}>` )
763779
return null // Failure - not allow duplicate by folderNameToMatch specs for the same folder folderNameToMatch
764780
} else {
765-
sortspecByName[folderNameToMatch] = spec
781+
collection.sortSpecByName![folderNameToMatch] = spec
766782
}
767783
}
768784
}
769785
}
770786

771-
if (sortspecByName) {
772-
collection = collection ?? newSortSpecsCollection()
773-
collection.sortSpecByName = sortspecByName
774-
}
775-
776-
let sortspecByWildcard: FolderWildcardMatching<CustomSortSpec> | undefined
777787
for (let spec of this.ctx.specs) {
778788
// Consume the folder paths ending with wildcard specs or regexp-based
779789
for (let idx = 0; idx<spec.targetFoldersPaths.length; idx++) {
780790
const path = spec.targetFoldersPaths[idx]
781791
if (path.startsWith(MatchFolderByRegexpLexeme)) {
782-
sortspecByWildcard = sortspecByWildcard ?? new FolderWildcardMatching<CustomSortSpec>()
792+
collection = ensureCollectionHasSortSpecByWildcard(collection)
783793
const folderByRegexpExpression: string = path.substring(MatchFolderByRegexpLexeme.length).trim()
784794
try {
785795
const r: ConsumedFolderMatchingRegexp = consumeFolderByRegexpExpression(folderByRegexpExpression)
786-
sortspecByWildcard.addRegexpDefinition(r.regexp, r.againstName, r.priority, r.log, spec)
796+
collection.sortSpecByWildcard!.addRegexpDefinition(r.regexp, r.againstName, r.priority, r.log, spec)
787797
} catch (e) {
788798
this.problem(ProblemCode.InvalidOrEmptyFolderMatchingRegexp,
789799
`Invalid or empty folder regexp expression <${folderByRegexpExpression}>`)
790800
return null
791801
}
792802
} else if (endsWithWildcardPatternSuffix(path)) {
793-
sortspecByWildcard = sortspecByWildcard ?? new FolderWildcardMatching<CustomSortSpec>()
794-
const ruleAdded = sortspecByWildcard.addWildcardDefinition(path, spec)
803+
collection = ensureCollectionHasSortSpecByWildcard(collection)
804+
const ruleAdded = collection.sortSpecByWildcard!.addWildcardDefinition(path, spec)
795805
if (ruleAdded?.errorMsg) {
796806
this.problem(ProblemCode.DuplicateWildcardSortSpecForSameFolder, ruleAdded?.errorMsg)
797807
return null // Failure - not allow duplicate wildcard specs for the same folder
@@ -800,16 +810,10 @@ export class SortingSpecProcessor {
800810
}
801811
}
802812

803-
if (sortspecByWildcard) {
804-
collection = collection ?? newSortSpecsCollection()
805-
collection.sortSpecByWildcard = sortspecByWildcard
806-
}
807-
808813
for (let spec of this.ctx.specs) {
809814
for (let idx = 0; idx < spec.targetFoldersPaths.length; idx++) {
810815
const originalPath = spec.targetFoldersPaths[idx]
811816
if (!originalPath.startsWith(MatchFolderNameLexeme) && !originalPath.startsWith(MatchFolderByRegexpLexeme)) {
812-
collection = collection ?? newSortSpecsCollection()
813817
const {path, detectedWildcardPriority} = stripWildcardPatternSuffix(originalPath)
814818
let storeTheSpec: boolean = true
815819
const preexistingSortSpecPriority: WildcardPriority = this.pathMatchPriorityForPath[path]
@@ -823,7 +827,8 @@ export class SortingSpecProcessor {
823827
}
824828
}
825829
if (storeTheSpec) {
826-
collection.sortSpecByPath[path] = spec
830+
collection = ensureCollectionHasSortSpecByPath(collection)
831+
collection.sortSpecByPath![path] = spec
827832
this.pathMatchPriorityForPath[path] = detectedWildcardPriority
828833
}
829834
}

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ export default class CustomSortPlugin extends Plugin {
334334

335335
// if custom sort is not specified, use the UI-selected
336336
const folder: TFolder = this.file
337-
let sortSpec: CustomSortSpec | null | undefined = plugin.sortSpecCache?.sortSpecByPath[folder.path]
338-
sortSpec = sortSpec ?? plugin.sortSpecCache?.sortSpecByName[folder.name]
337+
let sortSpec: CustomSortSpec | null | undefined = plugin.sortSpecCache?.sortSpecByPath?.[folder.path]
338+
sortSpec = sortSpec ?? plugin.sortSpecCache?.sortSpecByName?.[folder.name]
339339
if (sortSpec) {
340340
if (sortSpec.defaultOrder === CustomSortOrder.standardObsidian) {
341341
sortSpec = null // A folder is explicitly excluded from custom sorting plugin

0 commit comments

Comments
 (0)