-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodosearch.js
127 lines (93 loc) · 2.74 KB
/
todosearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const todoMarker=/^\s*- \[[ ,x]\]\s+/gm;
const markMarker=/\((ok|id):(\d+)\)/gm;
const todoWithTask =/^\s*[-,\*] \[([ ,x])\]\s+(\S.*)$/gm;
const todoTitle=/# Todo/gm;
const nextLine = /\n/gm;
const tagMarker=/#(\S+)/gm;
const tags = /#\S+/;
exports.findTags = (input) => {
var ret = [];
const matchIt = input.matchAll(tagMarker);
const matches = Array.from(matchIt);
for (var i = 0; i < matches.length ; i++){
const match = matches[i];
const tag = match[1].substr(1);
ret.push(match[1]);
}
return ret;
};
exports.findAll = (input,source) => {
var ret = [];
var match;
while (match = todoWithTask.exec(input)){
const done = match[1] == 'x';
const idx = todoWithTask.lastIndex - match[2].length;
ret.push({"index": idx ,"done": done, "item":match[2], "source": source});
}
return ret;
}
exports.findInsert = (input) => {
var insertLoc = input.length;
const matchIt = input.matchAll(todoMarker);
const matches = Array.from(matchIt);
if (matches && matches.length > 0){
const last = matches[matches.length - 1];
const lastLoc = last.index + last[0].length;
const nextLoc = input.substr(lastLoc).search(nextLine);
if (nextLoc >= 0) { insertLoc = nextLoc + lastLoc;}
}
else {
console.log('look for # Todo');
const matchT = input.matchAll(todoTitle);
const matches_t = Array.from(matchT);
if (matches_t && matches_t.length > 0){
console.log('found # Todo');
const last = matches_t[matches_t.length-1];
const lastLoc = last.index + last[0].length;
console.log(`${ lastLoc }`);
const nextLoc = input.substr(lastLoc).search(nextLine);
if (nextLoc >= 0) { insertLoc = nextLoc + lastLoc;}
}
}
return insertLoc;
};
/*
let test1 = `[[2020-08-29|<< Yesterday]] | [[2020-08-31|Tomorrow >>]]
# Energy
# Todo
- [ ] test
# Stuff
`;
let test2 = `[[2020-08-29|<< Yesterday]] | [[2020-08-31|Tomorrow >>]]
# Energy
# Todo
- [ ] test
# Stuff
- [ ] test2
`;
let testX = `[[2020-08-29|<< Yesterday]] | [[2020-08-31|Tomorrow >>]]
# Energy
# Todo
- [ ] test
# Stuff
- [ ] test2
* [ ] test3 - [ ]
- [x] test test test 4
* [ ] `;
let test0_res = exports.findAll(testX);
console.log(test0_res);
let test1_res = exports.findInsert(test1);
console.log(test1.length);
console.log(test1_res);
let test2_res = exports.findInsert(test2);
console.log(test2.length);
console.log(test2_res);
let test3 = `[[2020-08-29|<< Yesterday]] | [[2020-08-31|Tomorrow >>]]
# Energy
# Todo
# Stuff
`;
test3_res = exports.findInsert(test3);
console.log(test3.length);
console.log(test3_res);
*/