-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.js
189 lines (149 loc) · 4.15 KB
/
task.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
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
import { readFileSync, createReadStream } from 'node:fs'
import { stat } from 'node:fs/promises'
import { createInterface } from 'node:readline/promises'
import { join } from 'node:path'
import { marked } from 'marked'
export class Task {
#contents = null
constructor (filePath, metadata = {}) {
this.filePath = filePath
;[this.title, this.markdownTitle] = this.#parseTitle(metadata.title, metadata.filename)
this.description = metadata.description
this.created = metadata.created
this.modified = metadata.modified
this.relativePath = metadata.relativePath
this.filename = metadata.filename
this.lane = metadata.lane
}
get titleHTML () {
return marked.parse(this.title).trim()
}
get descriptionHTML () {
return marked.parse(this.description).trim()
}
render () {
if (!this.#contents) {
this.#contents = readFileSync(this.filePath, 'utf8')
}
const modifiedFileContents = this.#contents.split('\n')
modifiedFileContents[0] = this.markdownTitle
if (modifiedFileContents[1] && (modifiedFileContents[1].includes('--') || modifiedFileContents[1].includes('=='))) {
modifiedFileContents[1] = undefined
}
const content = marked.parse(modifiedFileContents.join('\n'))
return content.trim()
}
rawContents () {
if (!this.#contents) {
this.#contents = readFileSync(this.filePath, 'utf8')
}
return this.#contents
}
#parseTitle (title, filename) {
;[title, this.manualOrder] = this.#getManualOrder(title)
;[title, this.priority] = this.#getPriority(title)
;[title, this.tags] = this.#getTags(title)
if (!title) return [filename, '']
return [title.split('\n')[0].replace('#', '').trim(), title.trim()]
}
#getManualOrder (title) {
const manualOrderingRegex = /\((\d+)\)/g
let order
for (const match of title.matchAll(manualOrderingRegex)) {
// parse the ordering
const newOrder = Number(match[1])
if (newOrder > order || !order) {
order = newOrder
}
title = title.replace(match[0], '')
}
return [title, order]
}
#getPriority (title) {
const priorityRegex = /(!+)/g
let priority = 0
for (const match of title.matchAll(priorityRegex)) {
priority += match[1].length
title = title.slice(0, match.index) + title.slice(match.index + match[0].length)
}
return [title, priority]
}
#getTags (title) {
const tagsRegex = /\[([^\]]+)\]/g
const tags = []
for (const tag of title.matchAll(tagsRegex)) {
tags.push(tag[1])
title = title.replace(tag[0], '')
}
return [title, tags]
}
}
/**
*
* @param filePath
* @param dirPath
*/
export default async function TaskFactory (filePath, dirPath) {
const [title, description] = await getHeader(filePath)
const { birthtime: created, mtime: modified } = await stat(filePath)
const relativePath = filePath.replace(join(dirPath, '/'), '')
const [, lane, filename] = filePath.split(dirPath)[1].split('/')
return new Task(join(filePath), {
filename,
lane,
title,
description,
created,
modified,
relativePath,
})
}
/**
*
* @param filePath
*/
async function getHeader (filePath) {
const { promise, resolve, reject } = Promise.withResolvers()
const fileStream = createReadStream(filePath)
const rl = createInterface({ input: fileStream })
let title = ''
let description = ''
let firstEmptyLineReached = false
rl.on('line', (line) => {
// done
if (description) {
return rl.close()
}
// empty line
if (line === '') {
firstEmptyLineReached = true
return
}
if (firstEmptyLineReached === true) {
description = line
return
}
if (line.includes('#')) {
title = line
return
}
if (title) {
if (line.includes('--')) {
title = '## ' + title
firstEmptyLineReached = true
return
}
if (line.includes('==')) {
title = '# ' + title
firstEmptyLineReached = true
return
}
}
title = line
})
rl.on('close', () => {
resolve([title, description])
})
rl.on('error', (err) => reject(err))
return promise
}