-
Notifications
You must be signed in to change notification settings - Fork 0
/
tifa.ts
282 lines (247 loc) · 8.6 KB
/
tifa.ts
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
class tifa {
// !!! ALL METHODS WITH 'render' PREFIX ARE CALLED FROM OBSIDIAN !!!
// !!! RENAMING A 'render' METHOD WILL AFFECT ALL PAGES CALLING THE METHOD !!!
// !!! CHANGING A 'render' VARIABLES WILL AFFECT ALL PAGES CALLING THE METHOD !!!
// * GLOBAL
createCallout(type, message, content?: string | string[]) {
try {
// TODO: accomodate ordered and unordered lists
if (!type) {
throw new Error('createCallout() called without type')
}
let calloutString = `> [!${type}]`
if (message) {
calloutString += ` ${message}`
}
if (content) {
if (typeof content === 'string') {
calloutString += `\n${content}`
} else {
const joinedContent = content.join('\n > - ')
calloutString += `\n > - ${joinedContent}`
}
}
return calloutString
} catch(error) {
return this.handleError(error, 'createCallout()')
}
}
async getJSON(page, dv) {
// TODO: Add better error handling
// TODO: I need to know when the function fails
// TODO: I need to know when there is no JSON
// TODO: I need to know when the JSON is formatted incorrectly
try {
let content = await dv.io.load(page.file.path)
content = content.trim()
content = content.slice(3, -3)
const json = JSON.parse(content)
return json
} catch(error) {
return false
}
}
getWordCount(content) {
const count = content.split(' ').length
return count
}
async addImplicitProps(note, dv) {
const newNote = note
newNote.content = await dv.io.load(newNote.file.path)
newNote.wordCount = this.getWordCount(newNote.content)
return newNote
}
handleError(error: Error, location: string) {
return `> [!bug] Problem at ${location}\n${error}`
}
// * CALLOUTS
async renderCallouts(dv, callouts?: string | string[]) {
try {
if (callouts) {
// TODO: Handle specified callouts
dv.span(callouts)
} else {
const note = await this.addImplicitProps(dv.current(), dv)
const readTime = await this.getReadTime(note, dv)
if (readTime) {
dv.span(readTime)
}
await this.getNoteCallouts(note, dv)
}
} catch(error) {
dv.span(this.handleError(error, 'renderCallouts()'))
}
}
async getNoteCallouts(note, dv) {
try {
if(!note.type) {
dv.span(this.createCallout('missing', 'Missing Note Type'))
return
}
const schema = await this.getJSON(dv.page(note.type + '@callouts'), dv)
if (!schema) {
dv.span(this.createCallout('missing', `Missing [[Callout Schema]] | [[${note.type}@callouts |Add One]]`))
return
}
let content: string[] = []
let done: boolean = false
for (let key in schema) {
if (key === 'file') {
// for things like file property on a note
schema[key].forEach(rule => {
const passed = this.compareRule(note[key], rule)
if(!passed) {
rule.result.type ? dv.paragraph(this.createCallout(rule.result.type, rule.result.message, rule.result.content)) : content.push(rule.result.content)
if(rule.result.done) {
done = true
return
}
}
})
} else {
schema[key].forEach(rule => {
const passed = this.compareRule(note, rule)
if(!passed) {
rule.result.type ? dv.paragraph(this.createCallout(rule.result.type, rule.result.message, rule.result.content)) : content.push(rule.result.content)
if(rule.result.done) {
done = true
return
}
}
})
}
if (done) {
return
}
}
if (done) {
return
}
if (content.length !== 0) {
dv.paragraph(this.createCallout('warning', 'Needs Work', content))
}
} catch (error) {
dv.span(`> [!bug] Problem with getNoteCallouts()\n${error}`)
}
}
compareRule(note, rule) {
try {
// TODO: Handle more types than numbers
const property = rule.property
const condition = rule.condition
if(!note[property]) {
throw new Error(`${property} property does not exist`)
}
if (condition[1] === ' ') {
let operator = condition[0]
let value = condition.slice(-(condition.length - 2))
if (operator === '<') {
return note[property] > value
} else if (operator === '>') {
return note[property] < value
} else if (operator === '<=') {
return note[property] >= value
} else if (operator === '>=') {
return note[property] <= value
} else if (operator === '!') {
return note[property] == value
} else if (operator === '=') {
return note[property] !== value
}
}
if (typeof condition === 'string') {
return note[property] === condition
}
} catch(error) {
return `> [!bug] Problem with compareRule()\n${error}`
}
}
// * TASKS
// TODO: handle queries and display of tasks
async renderTasks(dv, option) {
const tasks = dv.pages('"Journal"').file.tasks
const headingLevel = 3
// Add task due today
const dueToday = tasks.filter(task => task.due && task.due.day === dv.parse(dv.current().file.name).day && task.due.month === dv.parse(dv.current().file.name).month && task.due.year === dv.parse(dv.current().file.name).year)
this.createTaskList(dv, dueToday, 'Due Today', headingLevel)
// Past due highlighted in red
const pastDue = tasks.filter(task => task.due && task.due < dv.parse(dv.current().file.name) && !task.completed)
this.createTaskList(dv, pastDue, 'Past Due', headingLevel)
// No due date warning
const noDueDate = tasks.filter(task => !task.due && !task.completed)
this.createTaskList(dv, noDueDate, 'NO DUE DATE', headingLevel)
// If all tasks complete, add backlog
if (dueToday.length === 2 && pastDue.length === 3) {
const futureTasks = tasks.filter(task => task.due && task.due > dv.parse(dv.current().file.name) && !task.completed)
dv.span('```ad-note\n' + '' + '\n```')
}
}
createTaskList(dv, tasks, heading, headingLevel) {
if (tasks.length > 0) {
dv.header(headingLevel, heading)
dv.taskList(tasks, false)
}
}
// * BIBLIOGRAPHY
// TODO: handle creating bibliography for topic based on references to book notes
renderBibliography(dv) {
try {
const references = this.formatReferences(this.getBookReferences(dv.current(), dv), dv)
const bibliography = `## Bibliography\n${references.join('\n')}`
dv.paragraph(bibliography)
} catch(error) {
dv.span(this.handleError(error, 'renderBibliography()'))
}
}
getBookReferences(noteData, dv) {
const outlinks = noteData.file.outlinks
let bookReferences = []
outlinks.forEach(link => {
if (dv.page(link).type === '📖') {
bookReferences.push(link)
}
})
return bookReferences
}
formatReferences(references, dv) {
const formattedReferences = []
references.forEach(reference => {
const info = dv.page(reference)
if(info.format === 'Book') {
formattedReferences.push(`${info.author}. *${info.title}*. ${info.publishLocation}: ${info.publisher}, ${info.publishDate}. [[${info.file.name} | ⬈]]`)
}
if(info.format === 'Journal Article') {
formattedReferences.push(`${info.author}. "*${info.title}*." ${info.publisher} ${info.issue} (${info.publishDate}): ${info.pages} [[${info.file.name} | ⬈]]`)
}
})
return formattedReferences
}
// * ESTIMATED READ TIME
async getReadTime(note, dv) {
try {
const content = await dv.io.load(note.file.path)
const wordCount = this.getWordCount(content)
const readTime = Math.round(wordCount/200)
let hourText: string = ''
let minText: string = ''
let hours: string|number = ''
let mins: string|number = ''
// set mins and hours
if (readTime > 60) {
hours = Math.floor(readTime / 60)
mins = readTime % 60
} else {
mins = readTime
}
// set hour text
if (hours) {
hours === 1 ? hourText = 'hr' : hourText = 'hrs'
}
// set min text
mins === 1 ? minText = 'min' : minText = 'mins'
return `>[!info] READ TIME: ${hours} ${hourText} ${mins} ${minText}`
} catch (error) {
dv.span(this.handleError(error, 'getReadTime()'))
}
}
}