2023-06-11 15:37:49 +00:00
|
|
|
import {getProjectFromPrefix, PrefixMode} from '@/modules/parseTaskText'
|
2022-12-02 17:39:52 +00:00
|
|
|
|
2022-09-28 17:46:12 +00:00
|
|
|
export interface TaskWithParent {
|
|
|
|
title: string,
|
|
|
|
parent: string | null,
|
2023-03-16 18:24:54 +00:00
|
|
|
project: string | null,
|
2022-09-28 17:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function cleanupTitle(title: string) {
|
|
|
|
return title.replace(/^((\* |\+ |- )(\[ \] )?)/g, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
const spaceRegex = /^ */
|
|
|
|
|
2022-09-30 11:36:57 +00:00
|
|
|
/**
|
|
|
|
* @param taskTitles should be multiple lines of task tiles with indention to declare their parent/subtask
|
|
|
|
* relation between each other.
|
|
|
|
*/
|
2023-06-11 15:37:49 +00:00
|
|
|
export function parseSubtasksViaIndention(taskTitles: string, prefixMode: PrefixMode): TaskWithParent[] {
|
2022-09-28 17:46:12 +00:00
|
|
|
const titles = taskTitles.split(/[\r\n]+/)
|
|
|
|
|
2022-09-29 16:08:05 +00:00
|
|
|
return titles.map((title, index) => {
|
2022-09-28 17:46:12 +00:00
|
|
|
const task: TaskWithParent = {
|
2022-09-29 16:08:05 +00:00
|
|
|
title: cleanupTitle(title),
|
2022-09-28 17:46:12 +00:00
|
|
|
parent: null,
|
2023-03-16 18:24:54 +00:00
|
|
|
project: null,
|
2022-09-28 17:46:12 +00:00
|
|
|
}
|
|
|
|
|
2023-06-11 15:37:49 +00:00
|
|
|
task.project = getProjectFromPrefix(task.title, prefixMode)
|
2022-12-02 17:39:52 +00:00
|
|
|
|
2022-09-29 16:09:35 +00:00
|
|
|
if (index === 0) {
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
2022-09-29 16:08:05 +00:00
|
|
|
const matched = spaceRegex.exec(title)
|
2022-09-28 17:46:12 +00:00
|
|
|
const matchedSpaces = matched ? matched[0].length : 0
|
|
|
|
|
2022-09-29 16:09:35 +00:00
|
|
|
if (matchedSpaces > 0) {
|
2022-09-28 17:46:12 +00:00
|
|
|
// Go up the tree to find the first task with less indention than the current one
|
|
|
|
let pi = 1
|
|
|
|
let parentSpaces = 0
|
|
|
|
do {
|
2022-09-29 16:08:05 +00:00
|
|
|
task.parent = cleanupTitle(titles[index - pi])
|
2022-09-28 17:46:12 +00:00
|
|
|
pi++
|
|
|
|
const parentMatched = spaceRegex.exec(task.parent)
|
|
|
|
parentSpaces = parentMatched ? parentMatched[0].length : 0
|
|
|
|
} while (parentSpaces >= matchedSpaces)
|
2022-09-29 16:08:05 +00:00
|
|
|
task.title = cleanupTitle(title.replace(spaceRegex, ''))
|
2022-09-28 17:46:12 +00:00
|
|
|
task.parent = task.parent.replace(spaceRegex, '')
|
2023-03-16 18:24:54 +00:00
|
|
|
if (task.project === null) {
|
2023-03-18 07:41:36 +00:00
|
|
|
// This allows to specify a project once for the parent task and inherit it to all subtasks
|
2023-06-11 15:37:49 +00:00
|
|
|
task.project = getProjectFromPrefix(task.parent, prefixMode)
|
2022-12-02 17:39:52 +00:00
|
|
|
}
|
2022-09-28 17:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return task
|
|
|
|
})
|
|
|
|
}
|