From f444614ddc32ec1bdeda1f4645be2867b5502c83 Mon Sep 17 00:00:00 2001 From: SebastianMC <23032356+SebastianMC@users.noreply.github.com> Date: Wed, 15 Feb 2023 22:27:57 +0100 Subject: [PATCH] #58 - Some target-folder: get ignored when sorting specs are read from two or more notes - fixed the bug --- .../sorting-spec-processor.spec.ts | 2 +- src/custom-sort/sorting-spec-processor.ts | 59 ++++++++++--------- src/main.ts | 4 +- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/custom-sort/sorting-spec-processor.spec.ts b/src/custom-sort/sorting-spec-processor.spec.ts index de04efa..623f4e0 100644 --- a/src/custom-sort/sorting-spec-processor.spec.ts +++ b/src/custom-sort/sorting-spec-processor.spec.ts @@ -930,7 +930,7 @@ describe('SortingSpecProcessor target-folder by name and regex', () => { it('should correctly handle the by-name only target-folder', () => { const inputTxtArr: Array = txtInputTargetFolderByName.split('\n') const result = processor.parseSortSpecFromText(inputTxtArr, 'mock-folder', 'custom-name-note.md') - expect(result?.sortSpecByPath).toEqual({}) + expect(result?.sortSpecByPath).toBeUndefined() expect(result?.sortSpecByName).toEqual(expectedSortSpecsTargetFolderByName) expect(result?.sortSpecByWildcard).not.toBeNull() }) diff --git a/src/custom-sort/sorting-spec-processor.ts b/src/custom-sort/sorting-spec-processor.ts index 3aeb28c..6a5e335 100644 --- a/src/custom-sort/sorting-spec-processor.ts +++ b/src/custom-sort/sorting-spec-processor.ts @@ -528,16 +528,33 @@ export interface FolderNameToSortSpecMap { } export interface SortSpecsCollection { - sortSpecByPath: FolderPathToSortSpecMap - sortSpecByName: FolderNameToSortSpecMap + sortSpecByPath?: FolderPathToSortSpecMap + sortSpecByName?: FolderNameToSortSpecMap sortSpecByWildcard?: FolderWildcardMatching } -export const newSortSpecsCollection = (): SortSpecsCollection => { - return { - sortSpecByPath: {}, - sortSpecByName: {} +const ensureCollectionHasSortSpecByPath = (collection?: SortSpecsCollection | null) => { + collection = collection ?? {} + if (!collection.sortSpecByPath) { + collection.sortSpecByPath = {} } + return collection +} + +const ensureCollectionHasSortSpecByName = (collection?: SortSpecsCollection | null) => { + collection = collection ?? {} + if (!collection.sortSpecByName) { + collection.sortSpecByName = {} + } + return collection +} + +const ensureCollectionHasSortSpecByWildcard = (collection?: SortSpecsCollection | null) => { + collection = collection ?? {} + if (!collection.sortSpecByWildcard) { + collection.sortSpecByWildcard = new FolderWildcardMatching() + } + return collection } interface AdjacencyInfo { @@ -744,7 +761,6 @@ export class SortingSpecProcessor { } } - let sortspecByName: FolderNameToSortSpecMap | undefined for (let spec of this.ctx.specs) { // Consume the folder names prefixed by the designated lexeme for (let idx = 0; idx` ) return null // Failure - not allow duplicate by folderNameToMatch specs for the same folder folderNameToMatch } else { - sortspecByName[folderNameToMatch] = spec + collection.sortSpecByName![folderNameToMatch] = spec } } } } - if (sortspecByName) { - collection = collection ?? newSortSpecsCollection() - collection.sortSpecByName = sortspecByName - } - - let sortspecByWildcard: FolderWildcardMatching | undefined for (let spec of this.ctx.specs) { // Consume the folder paths ending with wildcard specs or regexp-based for (let idx = 0; idx() + collection = ensureCollectionHasSortSpecByWildcard(collection) const folderByRegexpExpression: string = path.substring(MatchFolderByRegexpLexeme.length).trim() try { const r: ConsumedFolderMatchingRegexp = consumeFolderByRegexpExpression(folderByRegexpExpression) - sortspecByWildcard.addRegexpDefinition(r.regexp, r.againstName, r.priority, r.log, spec) + collection.sortSpecByWildcard!.addRegexpDefinition(r.regexp, r.againstName, r.priority, r.log, spec) } catch (e) { this.problem(ProblemCode.InvalidOrEmptyFolderMatchingRegexp, `Invalid or empty folder regexp expression <${folderByRegexpExpression}>`) return null } } else if (endsWithWildcardPatternSuffix(path)) { - sortspecByWildcard = sortspecByWildcard ?? new FolderWildcardMatching() - const ruleAdded = sortspecByWildcard.addWildcardDefinition(path, spec) + collection = ensureCollectionHasSortSpecByWildcard(collection) + const ruleAdded = collection.sortSpecByWildcard!.addWildcardDefinition(path, spec) if (ruleAdded?.errorMsg) { this.problem(ProblemCode.DuplicateWildcardSortSpecForSameFolder, ruleAdded?.errorMsg) return null // Failure - not allow duplicate wildcard specs for the same folder @@ -800,16 +810,10 @@ export class SortingSpecProcessor { } } - if (sortspecByWildcard) { - collection = collection ?? newSortSpecsCollection() - collection.sortSpecByWildcard = sortspecByWildcard - } - for (let spec of this.ctx.specs) { for (let idx = 0; idx < spec.targetFoldersPaths.length; idx++) { const originalPath = spec.targetFoldersPaths[idx] if (!originalPath.startsWith(MatchFolderNameLexeme) && !originalPath.startsWith(MatchFolderByRegexpLexeme)) { - collection = collection ?? newSortSpecsCollection() const {path, detectedWildcardPriority} = stripWildcardPatternSuffix(originalPath) let storeTheSpec: boolean = true const preexistingSortSpecPriority: WildcardPriority = this.pathMatchPriorityForPath[path] @@ -823,7 +827,8 @@ export class SortingSpecProcessor { } } if (storeTheSpec) { - collection.sortSpecByPath[path] = spec + collection = ensureCollectionHasSortSpecByPath(collection) + collection.sortSpecByPath![path] = spec this.pathMatchPriorityForPath[path] = detectedWildcardPriority } } diff --git a/src/main.ts b/src/main.ts index 11ea059..093d27a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -334,8 +334,8 @@ export default class CustomSortPlugin extends Plugin { // if custom sort is not specified, use the UI-selected const folder: TFolder = this.file - let sortSpec: CustomSortSpec | null | undefined = plugin.sortSpecCache?.sortSpecByPath[folder.path] - sortSpec = sortSpec ?? plugin.sortSpecCache?.sortSpecByName[folder.name] + let sortSpec: CustomSortSpec | null | undefined = plugin.sortSpecCache?.sortSpecByPath?.[folder.path] + sortSpec = sortSpec ?? plugin.sortSpecCache?.sortSpecByName?.[folder.name] if (sortSpec) { if (sortSpec.defaultOrder === CustomSortOrder.standardObsidian) { sortSpec = null // A folder is explicitly excluded from custom sorting plugin