Skip to content

Commit c5e463b

Browse files
committed
Merge branch 'master' into 171-poc-metadata-value-extractors-idea
# Conflicts: # manifest.json # src/custom-sort/matchers.ts # src/test/unit/sorting-spec-processor.spec.ts
2 parents 99cea92 + d3e21b2 commit c5e463b

File tree

11 files changed

+142
-35
lines changed

11 files changed

+142
-35
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
> | :exclamation: Breaking changes in Obsidian - always use the newest version of the plugin|
22
> |----------------------------------------------|
3+
> | - Obsidian 1.7.2 - no fixes for Obsidian breaking changes available --> [More details](README.notice.for.Obsidian.1.7.2.md)|
34
> | - Obsidian 1.6.3 - update the plugin to 2.1.11 or newer --> [More details](README.notice.for.Obsidian.1.6.3.md)|
45
> | - Obsidian 1.6.0 - update the plugin to 2.1.9 or newer --> [More details](README.notice.for.Obsidian.1.6.0.md)|
56
> | - Obsidian 1.5.4 - update the plugin to 2.1.7 or newer --> [More details](README.notice.for.Obsidian.1.5.4.md)|
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
> | :exclamation: Breaking changes in Obsidian 1.7.2 (and newer) - no fixes available|
2+
> |----------------------------------------------|
3+
>
4+
> Obsidian team introduced some more breaking changes in a not-backward-compatible way starting from Obsidian 1.7.2.
5+
>
6+
> The observed issues are minor, no fixes available (a work in progress with unknown release date):
7+
> - the custom sort plugin is **unable to automatically apply the custom sort on start**
8+
> - prevalent on mobile
9+
> - for the _Lazy Plugin Loader_ plugin occurs by definition
10+
> - **workaround**: custom sorting has to be applied manually via the ribbon icon or the command 'sort on'
11+
> - the custom sort plugin **keeps showing the notifications** with each change to any note
12+
> - **workaround**: disable the notifications
13+
>
14+
> For more details of the observed misbehaviors you can go to:
15+
> - [#161: Find out how to automatically apply custom sort on app start / vault (re)load etc.](https://github.com/SebastianMC/obsidian-custom-sort/issues/161)
16+
> - [#162: \[bug\]\[minor\] Obsidian 1.7.2 breaking changes - when File Explorer is not displayed an attempt to apply custom sort fails with error](https://github.com/SebastianMC/obsidian-custom-sort/issues/162)
17+
> - [#163: Obsidian 1.7.2 - automatic sorting fails when launching Obsidian](https://github.com/SebastianMC/obsidian-custom-sort/issues/163)
18+
> - [#165: Obsidian 1.7.3 - Constant Notifications "Custom sorting ON" and "Parsing custom sorting specification SUCCEEDED!"](https://github.com/SebastianMC/obsidian-custom-sort/issues/165)
19+
> - [#169: Not working on mobile](https://github.com/SebastianMC/obsidian-custom-sort/issues/169)
20+
>

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "custom-sort",
33
"name": "Custom File Explorer sorting",
4-
"version": "2.1.14-beta-171",
4+
"version": "2.1.15-",
55
"minAppVersion": "0.16.2",
66
"description": "Allows for manual and automatic, config-driven reordering and sorting of files and folders in File Explorer",
77
"author": "SebastianMC",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-custom-sort",
3-
"version": "2.1.14",
3+
"version": "2.1.15",
44
"description": "Custom Sort plugin for Obsidian (https://obsidian.md)",
55
"main": "main.js",
66
"scripts": {

src/custom-sort/custom-sort.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ const SortersForDerivedSecondary: { [key in CustomSortOrder]?: SorterFn } = {
241241
};
242242

243243
// OS - Obsidian Sort
244-
const OS_alphabetical = 'alphabetical'
244+
export const OS_alphabetical = 'alphabetical'
245245
const OS_alphabeticalReverse = 'alphabeticalReverse'
246246
export const OS_byModifiedTime = 'byModifiedTime'
247247
export const OS_byModifiedTimeReverse = 'byModifiedTimeReverse'
@@ -812,7 +812,7 @@ const folderSortCore = function (sortedFolder: TFolder, sortOrder: string, sorti
812812
};
813813

814814
// Returns a sorted copy of the input array, intentionally to keep it intact
815-
export const sortFolderItemsForBookmarking = function (folder: TFolder, items: Array<TAbstractFile>, sortingSpec: CustomSortSpec|null|undefined, ctx: ProcessingContext, uiSortOrder: string): Array<TAbstractFile> {
815+
export const sortFolderItems = function (folder: TFolder, items: Array<TAbstractFile>, sortingSpec: CustomSortSpec|null|undefined, ctx: ProcessingContext, uiSortOrder: string): Array<TAbstractFile> {
816816
if (sortingSpec) {
817817
const folderItemsByPath: { [key: string]: TAbstractFile } = {}
818818

@@ -849,6 +849,9 @@ export const sortFolderItemsForBookmarking = function (folder: TFolder, items: A
849849
}
850850
};
851851

852+
// Exported legacy function name for backward compatibility
853+
export const sortFolderItemsForBookmarking = sortFolderItems
854+
852855
export const _unitTests = {
853856
fileGoesFirstWhenSameBasenameAsFolder: fileGoesFirstWhenSameBasenameAsFolder,
854857
folderGoesFirstWhenSameBasenameAsFolder: folderGoesFirstWhenSameBasenameAsFolder

src/custom-sort/matchers.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import {toString} from "builtin-modules";
2-
31
export const RomanNumberRegexStr: string = ' *([MDCLXVI]+)'; // Roman number
42
export const CompoundRomanNumberDotRegexStr: string = ' *([MDCLXVI]+(?:\\.[MDCLXVI]+)*)';// Compound Roman number with dot as separator
53
export const CompoundRomanNumberDashRegexStr: string = ' *([MDCLXVI]+(?:-[MDCLXVI]+)*)'; // Compound Roman number with dash as separator
@@ -9,6 +7,7 @@ export const CompoundNumberDotRegexStr: string = ' *(\\d+(?:\\.\\d+)*)'; // Comp
97
export const CompoundNumberDashRegexStr: string = ' *(\\d+(?:-\\d+)*)'; // Compound number with dash as separator
108

119
export const Date_dd_Mmm_yyyy_RegexStr: string = ' *([0-3]*[0-9]-(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\\d{4})'; // Date like 01-Jan-2020
10+
export const Date_Mmm_dd_yyyy_RegexStr: string = ' *((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-[0-3]*[0-9]-\\d{4})'; // Date like Jan-01-2020
1211

1312
export const DOT_SEPARATOR = '.'
1413
export const DASH_SEPARATOR = '-'
@@ -123,16 +122,4 @@ export function getNormalizedDate_NormalizerFn_for(separator: string, dayIdx: nu
123122
}
124123

125124
export const getNormalizedDate_dd_Mmm_yyyy_NormalizerFn = getNormalizedDate_NormalizerFn_for('-', 0, 1, 2, MONTHS)
126-
127-
/*
128-
// Assumption - the regex date matched against input s, no extensive defensive coding needed
129-
const components = s.split('-')
130-
const day = prependWithZeros(components[0], DAY_POSITIONS)
131-
const month = prependWithZeros( `${1 + MONTHS.indexOf(components[1])}`, MONTH_POSITIONS)
132-
const year = prependWithZeros(components[2], YEAR_POSITIONS)
133-
return `${year}-${month}-${day}//`
134-
135-
*/
136-
137-
138-
125+
export const getNormalizedDate_Mmm_dd_yyyy_NormalizerFn = getNormalizedDate_NormalizerFn_for('-', 1, 0, 2, MONTHS)

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import {
1717
CompoundRomanNumberDotRegexStr,
1818
DASH_SEPARATOR,
1919
Date_dd_Mmm_yyyy_RegexStr,
20+
Date_Mmm_dd_yyyy_RegexStr,
2021
DOT_SEPARATOR,
2122
getNormalizedDate_dd_Mmm_yyyy_NormalizerFn,
23+
getNormalizedDate_Mmm_dd_yyyy_NormalizerFn,
2224
getNormalizedNumber,
2325
getNormalizedRomanNumber,
2426
NumberRegexStr,
@@ -358,6 +360,7 @@ const InlineRegexSymbol_Digit2: string = '\\[0-9]'
358360
const InlineRegexSymbol_0_to_3: string = '\\[0-3]'
359361

360362
const Date_dd_Mmm_yyyy_RegexSymbol: string = '\\[dd-Mmm-yyyy]'
363+
const Date_Mmm_dd_yyyy_RegexSymbol: string = '\\[Mmm-dd-yyyy]'
361364

362365
const InlineRegexSymbol_CapitalLetter: string = '\\C'
363366
const InlineRegexSymbol_LowercaseLetter: string = '\\l'
@@ -377,7 +380,8 @@ const sortingSymbolsArr: Array<string> = [
377380
escapeRegexUnsafeCharacters(CompoundRomanNumberDashRegexSymbol),
378381
escapeRegexUnsafeCharacters(WordInASCIIRegexSymbol),
379382
escapeRegexUnsafeCharacters(WordInAnyLanguageRegexSymbol),
380-
escapeRegexUnsafeCharacters(Date_dd_Mmm_yyyy_RegexSymbol)
383+
escapeRegexUnsafeCharacters(Date_dd_Mmm_yyyy_RegexSymbol),
384+
escapeRegexUnsafeCharacters(Date_Mmm_dd_yyyy_RegexSymbol)
381385
]
382386

383387
const sortingSymbolsRegex = new RegExp(sortingSymbolsArr.join('|'), 'gi')
@@ -446,6 +450,7 @@ export const NumberNormalizerFn: NormalizerFn = (s: string) => getNormalizedNumb
446450
export const CompoundDotNumberNormalizerFn: NormalizerFn = (s: string) => getNormalizedNumber(s, DOT_SEPARATOR)
447451
export const CompoundDashNumberNormalizerFn: NormalizerFn = (s: string) => getNormalizedNumber(s, DASH_SEPARATOR)
448452
export const Date_dd_Mmm_yyyy_NormalizerFn: NormalizerFn = (s: string) => getNormalizedDate_dd_Mmm_yyyy_NormalizerFn(s)
453+
export const Date_Mmm_dd_yyyy_NormalizerFn: NormalizerFn = (s: string) => getNormalizedDate_Mmm_dd_yyyy_NormalizerFn(s)
449454

450455
export enum AdvancedRegexType {
451456
None, // to allow if (advancedRegex)
@@ -457,7 +462,8 @@ export enum AdvancedRegexType {
457462
CompoundDashRomanNumber,
458463
WordInASCII,
459464
WordInAnyLanguage,
460-
Date_dd_Mmm_yyyy
465+
Date_dd_Mmm_yyyy,
466+
Date_Mmm_dd_yyyy
461467
}
462468

463469
const sortingSymbolToRegexpStr: { [key: string]: RegExpSpecStr } = {
@@ -506,6 +512,11 @@ const sortingSymbolToRegexpStr: { [key: string]: RegExpSpecStr } = {
506512
regexpStr: Date_dd_Mmm_yyyy_RegexStr,
507513
normalizerFn: Date_dd_Mmm_yyyy_NormalizerFn,
508514
advancedRegexType: AdvancedRegexType.Date_dd_Mmm_yyyy
515+
},
516+
[Date_Mmm_dd_yyyy_RegexSymbol]: { // Intentionally retain character case
517+
regexpStr: Date_Mmm_dd_yyyy_RegexStr,
518+
normalizerFn: Date_Mmm_dd_yyyy_NormalizerFn,
519+
advancedRegexType: AdvancedRegexType.Date_Mmm_dd_yyyy
509520
}
510521
}
511522

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
TAbstractFile,
3+
TFolder,
4+
Vault
5+
} from "obsidian";
6+
import {
7+
DEFAULT_FOLDER_CTIME,
8+
determineFolderDatesIfNeeded,
9+
determineSortingGroup,
10+
FolderItemForSorting, OS_alphabetical, OS_byCreatedTime, ProcessingContext, sortFolderItems
11+
} from "../../custom-sort/custom-sort";
12+
import {
13+
CustomSortGroupType,
14+
CustomSortOrder,
15+
CustomSortSpec
16+
} from "../../custom-sort/custom-sort-types";
17+
import {
18+
TIMESTAMP_OLDEST,
19+
TIMESTAMP_NEWEST,
20+
mockTFolderWithChildren,
21+
mockTFolderWithDateNamedChildren,
22+
TIMESTAMP_DEEP_NEWEST,
23+
TIMESTAMP_DEEP_OLDEST,
24+
} from "../mocks";
25+
import {
26+
SortingSpecProcessor
27+
} from "../../custom-sort/sorting-spec-processor";
28+
29+
describe('sortFolderItems', () => {
30+
it('should correctly handle Mmm-dd-yyyy pattern in file names', () => {
31+
// given
32+
const processor: SortingSpecProcessor = new SortingSpecProcessor()
33+
const sortSpecTxt =
34+
` ... \\[Mmm-dd-yyyy]
35+
> a-z
36+
`
37+
const PARENT_PATH = 'parent/folder/path'
38+
const sortSpecsCollection = processor.parseSortSpecFromText(
39+
sortSpecTxt.split('\n'),
40+
PARENT_PATH,
41+
'file name with the sorting, irrelevant here'
42+
)
43+
44+
const folder: TFolder = mockTFolderWithDateNamedChildren(PARENT_PATH)
45+
const sortSpec: CustomSortSpec = sortSpecsCollection?.sortSpecByPath![PARENT_PATH]!
46+
47+
const ctx: ProcessingContext = {}
48+
49+
// when
50+
const result: Array<TAbstractFile> = sortFolderItems(folder, folder.children, sortSpec, ctx, OS_alphabetical)
51+
52+
// then
53+
const orderedNames = result.map(f => f.name)
54+
expect(orderedNames).toEqual([
55+
'CCC Feb-28-2025',
56+
'BBB Dec-23-2024.md',
57+
'DDD Jul-15-2024.md',
58+
'AAA Jan-01-2012'
59+
])
60+
})
61+
})
62+
63+
64+

src/test/mocks.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {
33
TFolder,
44
Vault
55
} from "obsidian";
6+
import {
7+
lastPathComponent
8+
} from "../utils/utils";
69

710
export const mockTFile = (basename: string, ext: string, size?: number, ctime?: number, mtime?: number): TFile => {
811
return {
@@ -25,7 +28,7 @@ export const mockTFolder = (name: string, children?: Array<TFolder|TFile>, paren
2528
isRoot(): boolean { return name === '/' },
2629
vault: {} as Vault, // To satisfy TS typechecking
2730
path: `${name}`,
28-
name: name,
31+
name: lastPathComponent(name),
2932
parent: parent ?? ({} as TFolder), // To satisfy TS typechecking
3033
children: children ?? []
3134
}
@@ -53,3 +56,12 @@ export const mockTFolderWithChildren = (name: string): TFolder => {
5356

5457
return mockTFolder(name, [child1, child2, child3, child4, child5, subfolder1, subfolder2])
5558
}
59+
60+
export const mockTFolderWithDateNamedChildren = (name: string): TFolder => {
61+
const child1: TFolder = mockTFolder('AAA Jan-01-2012')
62+
const child2: TFile = mockTFile('BBB Dec-23-2024', 'md')
63+
const child3: TFolder = mockTFolder('CCC Feb-28-2025')
64+
const child4: TFile = mockTFile('DDD Jul-15-2024', 'md')
65+
66+
return mockTFolder(name, [child1, child2, child3, child4])
67+
}

src/test/unit/sorting-spec-processor.spec.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
consumeFolderByRegexpExpression,
77
convertPlainStringToRegex,
88
Date_dd_Mmm_yyyy_NormalizerFn,
9+
Date_Mmm_dd_yyyy_NormalizerFn,
910
detectSortingSymbols,
1011
escapeRegexUnsafeCharacters,
1112
extractSortingSymbol,
@@ -363,17 +364,6 @@ const expectedSortSpecsExampleA: { [key: string]: CustomSortSpec } = {
363364
}
364365
}
365366

366-
const txtInputExampleSortingSymbols: string = `
367-
/folders Chapter \\.d+ ...
368-
/:files ...section \\-r+.
369-
% Appendix \\-d+ (attachments)
370-
Plain syntax\\R+ ... works?
371-
And this kind of... \\D+plain syntax???
372-
Here goes ASCII word \\a+
373-
\\A+. is for any modern language word
374-
\\[dd-Mmm-yyyy] for the specific date format of 12-Apr-2024
375-
`
376-
377367
const expectedSortSpecsExampleSortingSymbols: { [key: string]: CustomSortSpec } = {
378368
"mock-folder": {
379369
groups: [{
@@ -428,14 +418,32 @@ const expectedSortSpecsExampleSortingSymbols: { [key: string]: CustomSortSpec }
428418
regex: /^ *([0-3]*[0-9]-(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4}) for the specific date format of 12\-Apr\-2024$/i,
429419
normalizerFn: Date_dd_Mmm_yyyy_NormalizerFn
430420
}
421+
}, {
422+
type: CustomSortGroupType.ExactName,
423+
regexPrefix: {
424+
regex: /^ *((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-[0-3]*[0-9]-\d{4}) for the specific date format of Apr\-01\-2024$/i,
425+
normalizerFn: Date_Mmm_dd_yyyy_NormalizerFn
426+
}
431427
}, {
432428
type: CustomSortGroupType.Outsiders
433429
}],
434430
targetFoldersPaths: ['mock-folder'],
435-
outsidersGroupIdx: 8
431+
outsidersGroupIdx: 9
436432
}
437433
}
438434

435+
const txtInputExampleSortingSymbols: string = `
436+
/folders Chapter \\.d+ ...
437+
/:files ...section \\-r+.
438+
% Appendix \\-d+ (attachments)
439+
Plain syntax\\R+ ... works?
440+
And this kind of... \\D+plain syntax???
441+
Here goes ASCII word \\a+
442+
\\A+. is for any modern language word
443+
\\[dd-Mmm-yyyy] for the specific date format of 12-Apr-2024
444+
\\[Mmm-dd-yyyy] for the specific date format of Apr-01-2024
445+
`
446+
439447
const txtInputExampleMDataExtractors1: string = `
440448
< a-z by-metadata: created-by using-extractor: date(dd/mm/yyyy)
441449
/folders Chapter...

0 commit comments

Comments
 (0)