-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
141 lines (125 loc) · 5.48 KB
/
index.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
import {TodoistApi} from "@doist/todoist-api-typescript";
export type TodoistProject = { 'project_name': string, 'project_tree': string };
export enum ItemType {
PROJECT,
SECTION,
TASK
}
class TodoistItem {
type: ItemType;
id: string;
name: string;
parentId: string | null;
order: number;
commentCount: number;
constructor(type: ItemType, id: string, name: string, parentId: string | null, order = 0, commentCount = 0) {
this.type = type;
this.id = id;
this.name = name;
this.parentId = parentId;
this.order = order;
this.commentCount = commentCount;
}
}
export class TodoistNode extends TodoistItem {
children: TodoistNode[];
constructor(type: ItemType, id: string, name: string, parentId: string | null, children: TodoistNode[]) {
super(type, id, name, parentId);
this.children = children;
}
}
export class TodoistComment {
id: string;
content: string;
parentId: string;
constructor(id: string, content: string, parentId: string) {
this.id = id;
this.content = content;
this.parentId = parentId;
}
}
const itemCompare = (a: TodoistItem, b: TodoistItem) => a.order - b.order;
export const createTree = (currentNode: TodoistNode, allNodes: TodoistNode[]): TodoistNode => {
const children = [];
for (const child of allNodes.filter(item => item.parentId == currentNode.id)) {
children.push(createTree(child, allNodes));
}
return new TodoistNode(currentNode.type, currentNode.id, currentNode.name, currentNode.parentId, children);
}
export const formatTree = (currentNode: TodoistNode, comments: TodoistComment[], indent: number = 0): string => {
const formattedIndent: string = currentNode.type === ItemType.TASK ? `${'\t'.repeat(indent)}- [ ] ` : '';
let formattedName: string;
switch (currentNode.type) {
case ItemType.PROJECT:
formattedName = `# ${currentNode.name}`;
break;
case ItemType.SECTION:
formattedName = `## ${currentNode.name}`;
break;
default:
formattedName = currentNode.name;
break;
}
const currentComments = comments
.filter(comment => comment.parentId == currentNode.id)
.map(comment => comment.content);
const formattedComments = currentComments.length > 0 ? `_${currentComments.join(' ')}_` : '';
const incIndent = currentNode.type === ItemType.TASK ? indent + 1 : indent;
const formattedChildren: string = currentNode.children
.map(child => formatTree(child, comments, incIndent)).join('');
return `${formattedIndent}${formattedName} ${formattedComments}\n${formattedChildren}`;
}
class TodoistToMarkdown {
private readonly projects: TodoistItem[];
private readonly sections: TodoistItem[];
private readonly tasks: TodoistItem[];
private readonly comments: TodoistComment[];
private constructor(projects: TodoistItem[] = [], sections: TodoistItem[] = [], tasks: TodoistItem[] = [], comments: TodoistComment[] = []) {
this.projects = projects;
this.sections = sections;
this.tasks = tasks;
this.comments = comments;
}
static async initialize(token: string) {
const api = new TodoistApi(token);
const projects: TodoistItem[] = (await api.getProjects())
.map(project => new TodoistItem(ItemType.PROJECT, project.id, project.name, project.parentId || null, project.order, project.commentCount));
projects.sort(itemCompare);
const sections: TodoistItem[] = (await api.getSections())
.map(section => new TodoistItem(ItemType.SECTION, section.id, section.name, section.projectId, section.order));
sections.sort(itemCompare);
const tasks: TodoistItem[] = (await api.getTasks())
.map(task => new TodoistItem(ItemType.TASK, task.id, task.content, task.parentId || task.sectionId || task.projectId || null, task.order, task.commentCount));
tasks.sort(itemCompare);
const comments: TodoistComment[] = [];
for (const project of projects) {
if (project.commentCount > 0) {
comments.concat((await api.getComments({projectId: project.id}))
.map(comment => new TodoistComment(comment.id, comment.content, project.id)));
}
}
for (const task of tasks) {
if (task.commentCount > 0) {
comments.concat((await api.getComments({taskId: task.id}))
.map(comment => new TodoistComment(comment.id, comment.content, task.id)));
}
}
return new TodoistToMarkdown(projects, sections, tasks, comments);
}
convert(): TodoistProject[] {
const allItems = this.projects.concat(this.sections).concat(this.tasks)
.map(item => item as TodoistNode);
let projectTrees: TodoistProject[] = [];
const rootProjects = this.projects.filter(project => project.parentId == null)
.map(item => item as TodoistNode);
for (const rootProject of rootProjects) {
const projectTree = createTree(rootProject, allItems);
const projectTreeStr = formatTree(projectTree, this.comments);
projectTrees.push({'project_name': rootProject.name, 'project_tree': projectTreeStr});
}
return projectTrees;
}
}
export async function todoistToMarkdown(token: string): Promise<TodoistProject[]> {
return (await TodoistToMarkdown.initialize(token)).convert();
}