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…
This commit is contained in:
commit
feed16cae2
|
@ -930,7 +930,7 @@ describe('SortingSpecProcessor target-folder by name and regex', () => {
|
||||||
it('should correctly handle the by-name only target-folder', () => {
|
it('should correctly handle the by-name only target-folder', () => {
|
||||||
const inputTxtArr: Array<string> = txtInputTargetFolderByName.split('\n')
|
const inputTxtArr: Array<string> = txtInputTargetFolderByName.split('\n')
|
||||||
const result = processor.parseSortSpecFromText(inputTxtArr, 'mock-folder', 'custom-name-note.md')
|
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?.sortSpecByName).toEqual(expectedSortSpecsTargetFolderByName)
|
||||||
expect(result?.sortSpecByWildcard).not.toBeNull()
|
expect(result?.sortSpecByWildcard).not.toBeNull()
|
||||||
})
|
})
|
||||||
|
|
|
@ -528,16 +528,33 @@ export interface FolderNameToSortSpecMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SortSpecsCollection {
|
export interface SortSpecsCollection {
|
||||||
sortSpecByPath: FolderPathToSortSpecMap
|
sortSpecByPath?: FolderPathToSortSpecMap
|
||||||
sortSpecByName: FolderNameToSortSpecMap
|
sortSpecByName?: FolderNameToSortSpecMap
|
||||||
sortSpecByWildcard?: FolderWildcardMatching<CustomSortSpec>
|
sortSpecByWildcard?: FolderWildcardMatching<CustomSortSpec>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const newSortSpecsCollection = (): SortSpecsCollection => {
|
const ensureCollectionHasSortSpecByPath = (collection?: SortSpecsCollection | null) => {
|
||||||
return {
|
collection = collection ?? {}
|
||||||
sortSpecByPath: {},
|
if (!collection.sortSpecByPath) {
|
||||||
sortSpecByName: {}
|
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<CustomSortSpec>()
|
||||||
|
}
|
||||||
|
return collection
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AdjacencyInfo {
|
interface AdjacencyInfo {
|
||||||
|
@ -744,7 +761,6 @@ export class SortingSpecProcessor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let sortspecByName: FolderNameToSortSpecMap | undefined
|
|
||||||
for (let spec of this.ctx.specs) {
|
for (let spec of this.ctx.specs) {
|
||||||
// Consume the folder names prefixed by the designated lexeme
|
// Consume the folder names prefixed by the designated lexeme
|
||||||
for (let idx = 0; idx<spec.targetFoldersPaths.length; idx++) {
|
for (let idx = 0; idx<spec.targetFoldersPaths.length; idx++) {
|
||||||
|
@ -756,42 +772,36 @@ export class SortingSpecProcessor {
|
||||||
`Empty '${TargetFolderLexeme} ${MatchFolderNameLexeme}' value` )
|
`Empty '${TargetFolderLexeme} ${MatchFolderNameLexeme}' value` )
|
||||||
return null // Failure - not allow duplicate by folderNameToMatch specs for the same folder folderNameToMatch
|
return null // Failure - not allow duplicate by folderNameToMatch specs for the same folder folderNameToMatch
|
||||||
}
|
}
|
||||||
sortspecByName = sortspecByName ?? {}
|
collection = ensureCollectionHasSortSpecByName(collection)
|
||||||
if (sortspecByName[folderNameToMatch]) {
|
if (collection.sortSpecByName![folderNameToMatch]) {
|
||||||
this.problem(ProblemCode.DuplicateByNameSortSpecForFolder,
|
this.problem(ProblemCode.DuplicateByNameSortSpecForFolder,
|
||||||
`Duplicate '${TargetFolderLexeme} ${MatchFolderNameLexeme}' definition for the same name <${folderNameToMatch}>` )
|
`Duplicate '${TargetFolderLexeme} ${MatchFolderNameLexeme}' definition for the same name <${folderNameToMatch}>` )
|
||||||
return null // Failure - not allow duplicate by folderNameToMatch specs for the same folder folderNameToMatch
|
return null // Failure - not allow duplicate by folderNameToMatch specs for the same folder folderNameToMatch
|
||||||
} else {
|
} else {
|
||||||
sortspecByName[folderNameToMatch] = spec
|
collection.sortSpecByName![folderNameToMatch] = spec
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sortspecByName) {
|
|
||||||
collection = collection ?? newSortSpecsCollection()
|
|
||||||
collection.sortSpecByName = sortspecByName
|
|
||||||
}
|
|
||||||
|
|
||||||
let sortspecByWildcard: FolderWildcardMatching<CustomSortSpec> | undefined
|
|
||||||
for (let spec of this.ctx.specs) {
|
for (let spec of this.ctx.specs) {
|
||||||
// Consume the folder paths ending with wildcard specs or regexp-based
|
// Consume the folder paths ending with wildcard specs or regexp-based
|
||||||
for (let idx = 0; idx<spec.targetFoldersPaths.length; idx++) {
|
for (let idx = 0; idx<spec.targetFoldersPaths.length; idx++) {
|
||||||
const path = spec.targetFoldersPaths[idx]
|
const path = spec.targetFoldersPaths[idx]
|
||||||
if (path.startsWith(MatchFolderByRegexpLexeme)) {
|
if (path.startsWith(MatchFolderByRegexpLexeme)) {
|
||||||
sortspecByWildcard = sortspecByWildcard ?? new FolderWildcardMatching<CustomSortSpec>()
|
collection = ensureCollectionHasSortSpecByWildcard(collection)
|
||||||
const folderByRegexpExpression: string = path.substring(MatchFolderByRegexpLexeme.length).trim()
|
const folderByRegexpExpression: string = path.substring(MatchFolderByRegexpLexeme.length).trim()
|
||||||
try {
|
try {
|
||||||
const r: ConsumedFolderMatchingRegexp = consumeFolderByRegexpExpression(folderByRegexpExpression)
|
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) {
|
} catch (e) {
|
||||||
this.problem(ProblemCode.InvalidOrEmptyFolderMatchingRegexp,
|
this.problem(ProblemCode.InvalidOrEmptyFolderMatchingRegexp,
|
||||||
`Invalid or empty folder regexp expression <${folderByRegexpExpression}>`)
|
`Invalid or empty folder regexp expression <${folderByRegexpExpression}>`)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
} else if (endsWithWildcardPatternSuffix(path)) {
|
} else if (endsWithWildcardPatternSuffix(path)) {
|
||||||
sortspecByWildcard = sortspecByWildcard ?? new FolderWildcardMatching<CustomSortSpec>()
|
collection = ensureCollectionHasSortSpecByWildcard(collection)
|
||||||
const ruleAdded = sortspecByWildcard.addWildcardDefinition(path, spec)
|
const ruleAdded = collection.sortSpecByWildcard!.addWildcardDefinition(path, spec)
|
||||||
if (ruleAdded?.errorMsg) {
|
if (ruleAdded?.errorMsg) {
|
||||||
this.problem(ProblemCode.DuplicateWildcardSortSpecForSameFolder, ruleAdded?.errorMsg)
|
this.problem(ProblemCode.DuplicateWildcardSortSpecForSameFolder, ruleAdded?.errorMsg)
|
||||||
return null // Failure - not allow duplicate wildcard specs for the same folder
|
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 spec of this.ctx.specs) {
|
||||||
for (let idx = 0; idx < spec.targetFoldersPaths.length; idx++) {
|
for (let idx = 0; idx < spec.targetFoldersPaths.length; idx++) {
|
||||||
const originalPath = spec.targetFoldersPaths[idx]
|
const originalPath = spec.targetFoldersPaths[idx]
|
||||||
if (!originalPath.startsWith(MatchFolderNameLexeme) && !originalPath.startsWith(MatchFolderByRegexpLexeme)) {
|
if (!originalPath.startsWith(MatchFolderNameLexeme) && !originalPath.startsWith(MatchFolderByRegexpLexeme)) {
|
||||||
collection = collection ?? newSortSpecsCollection()
|
|
||||||
const {path, detectedWildcardPriority} = stripWildcardPatternSuffix(originalPath)
|
const {path, detectedWildcardPriority} = stripWildcardPatternSuffix(originalPath)
|
||||||
let storeTheSpec: boolean = true
|
let storeTheSpec: boolean = true
|
||||||
const preexistingSortSpecPriority: WildcardPriority = this.pathMatchPriorityForPath[path]
|
const preexistingSortSpecPriority: WildcardPriority = this.pathMatchPriorityForPath[path]
|
||||||
|
@ -823,7 +827,8 @@ export class SortingSpecProcessor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (storeTheSpec) {
|
if (storeTheSpec) {
|
||||||
collection.sortSpecByPath[path] = spec
|
collection = ensureCollectionHasSortSpecByPath(collection)
|
||||||
|
collection.sortSpecByPath![path] = spec
|
||||||
this.pathMatchPriorityForPath[path] = detectedWildcardPriority
|
this.pathMatchPriorityForPath[path] = detectedWildcardPriority
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -334,8 +334,8 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
|
|
||||||
// if custom sort is not specified, use the UI-selected
|
// if custom sort is not specified, use the UI-selected
|
||||||
const folder: TFolder = this.file
|
const folder: TFolder = this.file
|
||||||
let sortSpec: CustomSortSpec | null | undefined = plugin.sortSpecCache?.sortSpecByPath[folder.path]
|
let sortSpec: CustomSortSpec | null | undefined = plugin.sortSpecCache?.sortSpecByPath?.[folder.path]
|
||||||
sortSpec = sortSpec ?? plugin.sortSpecCache?.sortSpecByName[folder.name]
|
sortSpec = sortSpec ?? plugin.sortSpecCache?.sortSpecByName?.[folder.name]
|
||||||
if (sortSpec) {
|
if (sortSpec) {
|
||||||
if (sortSpec.defaultOrder === CustomSortOrder.standardObsidian) {
|
if (sortSpec.defaultOrder === CustomSortOrder.standardObsidian) {
|
||||||
sortSpec = null // A folder is explicitly excluded from custom sorting plugin
|
sortSpec = null // A folder is explicitly excluded from custom sorting plugin
|
||||||
|
|
Loading…
Reference in New Issue