1
1
import { escapeRegExp , has , keysIn , set } from "lodash" ;
2
- import { DateTime , Interval } from "luxon" ;
2
+ import { DateTime } from "luxon" ;
3
3
import { DeepPartial , PickByValue } from "utility-types" ;
4
4
5
5
import { Task } from "@/model/task/schema" ;
6
- import { PathOf } from "@/util/type-utils" ;
6
+ import { PathsOf } from "@/util/type-utils" ;
7
7
8
8
/**
9
- * Parses {@link Task} metadata from a real markdown task using the Obsidian Task plugin 's syntax .
10
- * Specifically, the text is expected to find occurrences from the { @link SYMBOL_PATH_LOOKUP} and write to the
11
- * corresponding field using the proceeding text.
9
+ * Parses {@link Task} metadata from a real markdown blob using Obsidian Task's emoji format .
10
+ *
11
+ * @see { @link https://publish.obsidian.md/tasks/Reference/Task+Formats/Tasks+Emoji+Format }
12
12
* @example
13
+ *
13
14
* ```
14
- * "the text at the front is assumed to be a description. ❌ cancelled date ➕ created date ✅ completed date"
15
- * ( symbol & value )(symbol & value)( symbol & value )
15
+ * ( symbol & value )
16
+ * "the text at the front is assumed to be a description. ❌ cancelled date ➕ creation date ✅ completed date"
17
+ * ( symbol & value ) ( symbol & value )
18
+ *
19
+ * { cancelled: "cancelled date", created: "creation date", done: "completed date" }
16
20
* ```
17
- * @see { @link https://publish.obsidian.md/tasks/Reference/Task+Formats/Tasks+Emoji+Format }
21
+ *
18
22
* @param text - the text of the task without its' markdown text.
19
23
* @returns a {@link Task} with the parsed metadata.
20
24
*/
21
25
export function parseTaskEmojiFormat ( text : string ) : DeepPartial < Task > {
22
26
const matchedSymbols = [ ...text . matchAll ( SYMBOL_REG_EXP ) , / $ / . exec ( text ) as RegExpExecArray ] ;
23
27
const textBeforeAllSymbols = text . slice ( 0 , matchedSymbols [ 0 ] . index ) ;
24
28
25
- const result : DeepPartial < Task > = { ... parseTaskHeader ( textBeforeAllSymbols . trim ( ) ) } ;
29
+ const result : DeepPartial < Task > = { description : textBeforeAllSymbols . trim ( ) } ;
26
30
27
31
for ( let i = 0 ; i <= matchedSymbols . length - 2 ; ++ i ) {
28
32
const [ execArray , nextExecArray ] = matchedSymbols . slice ( i , i + 2 ) ;
@@ -46,27 +50,6 @@ export function parseTaskEmojiFormat(text: string): DeepPartial<Task> {
46
50
return result ;
47
51
}
48
52
49
- /**
50
- * Parses {@link Task} metadata from a task's header.
51
- * @param headerText - the text that appears _before_ all of the symbols.
52
- * @returns the {@link Task} metadata fields parsed from the header; unparsed data will become {@link Task.description}.
53
- */
54
- function parseTaskHeader ( headerText : string ) : DeepPartial < Task > {
55
- const [ isoString , isoStringSuffix ] = headerText . split ( / \s + / , 2 ) ;
56
-
57
- const interval = Interval . fromISO ( isoString ) ;
58
- if ( interval . isValid ) {
59
- return { times : { start : interval . start , end : interval . end } , description : isoStringSuffix } ;
60
- }
61
-
62
- const time = DateTime . fromISO ( isoString ) ;
63
- if ( time . isValid ) {
64
- return { times : { start : time } , description : isoStringSuffix } ;
65
- }
66
-
67
- return { description : headerText } ;
68
- }
69
-
70
53
const SYMBOL_PATH_LOOKUP = {
71
54
"❌" : "dates.cancelled" ,
72
55
"➕" : "dates.created" ,
@@ -82,15 +65,14 @@ const SYMBOL_PATH_LOOKUP = {
82
65
"🔼" : "priority" ,
83
66
"🔽" : "priority" ,
84
67
"⏬" : "priority" ,
85
- "🔁" : "recurrenceRule" ,
86
- } as const satisfies Record < string , PathOf < Task > > ;
68
+ } as const satisfies Record < string , PathsOf < Task > > ;
87
69
88
70
const SYMBOL_PRIORITY_LOOKUP = {
89
71
"🔺" : 0 ,
90
72
"⏫" : 1 ,
91
73
"🔼" : 2 ,
92
74
"🔽" : 4 ,
93
75
"⏬" : 5 ,
94
- } as const satisfies Record < keyof PickByValue < typeof SYMBOL_PATH_LOOKUP , "priority" > , number > ;
76
+ } as const satisfies { [ K in keyof PickByValue < typeof SYMBOL_PATH_LOOKUP , "priority" > ] : number } ;
95
77
96
78
const SYMBOL_REG_EXP = new RegExp ( keysIn ( SYMBOL_PATH_LOOKUP ) . map ( escapeRegExp ) . join ( "|" ) , "g" ) ;
0 commit comments