generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
226 lines (193 loc) · 4.95 KB
/
main.ts
File metadata and controls
226 lines (193 loc) · 4.95 KB
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
import {
App,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
TFile,
} from "obsidian";
interface TasksCleanerSettings {
daysThreshold: number;
taskPattern: string;
filenamePattern: string;
}
const DEFAULT_SETTINGS: TasksCleanerSettings = {
daysThreshold: 7,
taskPattern: "- \\[x\\].*?✅\\s*(\\d{4}-\\d{2}-\\d{2})",
filenamePattern: "TODO",
};
export default class TasksCleanerPlugin extends Plugin {
settings: TasksCleanerSettings;
async onload() {
await this.loadSettings();
this.addRibbonIcon("trash", "Remove old tasks", async () => {
await this.cleanOldTasks();
});
this.addCommand({
id: "tasks-cleaner-remove-old-tasks",
name: "Remove old tasks",
callback: async () => {
await this.cleanOldTasks();
},
});
this.addSettingTab(new TasksCleanerSettingTab(this.app, this));
new Notice("Tasks Cleaner plugin loaded.");
}
async cleanOldTasks() {
const files = this.app.vault.getMarkdownFiles();
const now = new Date();
const thresholdDate = new Date(
now.getTime() - this.settings.daysThreshold * 24 * 60 * 60 * 1000,
);
const taskRegex = new RegExp(this.settings.taskPattern);
const results: {
file: TFile;
linesToDelete: number[];
taskCount: number;
content: string;
}[] = [];
for (const file of files) {
if (
this.settings.filenamePattern &&
!file.name.includes(this.settings.filenamePattern)
)
continue;
const content = await this.app.vault.read(file);
const lines = content.split("\n");
const linesToDelete: number[] = [];
let taskCount = 0;
let i = 0;
while (i < lines.length) {
const line = lines[i];
const match = line.match(taskRegex);
if (match) {
const doneDate = new Date(match[1]);
if (doneDate < thresholdDate) {
linesToDelete.push(i);
taskCount++;
let j = i + 1;
while (j < lines.length && lines[j].match(/^\s+/)) {
linesToDelete.push(j);
j++;
}
i = j;
continue;
}
}
i++;
}
if (linesToDelete.length > 0) {
results.push({ file, linesToDelete, taskCount, content });
}
}
if (results.length === 0) {
new Notice("There are no tasks to delete.");
return;
}
new ConfirmModal(this.app, results, async () => {
for (const result of results) {
const newLines = result.content
.split("\n")
.filter((_, idx) => !result.linesToDelete.includes(idx));
await this.app.vault.modify(result.file, newLines.join("\n"));
}
new Notice("Outdated tasks have been deleted.");
}).open();
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData(),
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class ConfirmModal extends Modal {
constructor(
app: App,
private results: { file: TFile; taskCount: number }[],
private onConfirm: () => void,
) {
super(app);
}
onOpen() {
const { contentEl } = this;
contentEl.createEl("h2", { text: "Confirmation of deletion" });
const ul = contentEl.createEl("ul");
for (const { file, taskCount } of this.results) {
ul.createEl("li", {
text: `${file.path}: ${taskCount} tasks will be deleted`,
});
}
const button = contentEl.createEl("button", {
text: "Clear",
cls: "tasks-cleaner-button-clear",
});
button.onclick = () => {
this.onConfirm();
this.close();
};
}
onClose() {
this.contentEl.empty();
}
}
class TasksCleanerSettingTab extends PluginSettingTab {
plugin: TasksCleanerPlugin;
constructor(app: App, plugin: TasksCleanerPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
new Setting(containerEl)
.setName("Delete issues older than (days)")
.setDesc(
"Completed tasks older than this number of days will be deleted.",
)
.addText((text) =>
text
.setPlaceholder("for example, 7")
.setValue(this.plugin.settings.daysThreshold.toString())
.onChange(async (value) => {
const num = parseInt(value);
if (!isNaN(num)) {
this.plugin.settings.daysThreshold = num;
await this.plugin.saveSettings();
}
}),
);
new Setting(containerEl)
.setName("Task template")
.setDesc(
"A regular expression for searching for completed tasks. It must contain the completion date.",
)
.addText((text) =>
text
.setPlaceholder("- [x] ... ✅ yyyy-mm-dd")
.setValue(this.plugin.settings.taskPattern)
.onChange(async (value) => {
this.plugin.settings.taskPattern = value;
await this.plugin.saveSettings();
}),
);
new Setting(containerEl)
.setName("Filter by file name")
.setDesc(
"If specified, tasks will be cleared only in files containing this line in the name.",
)
.addText((text) =>
text
.setPlaceholder("TODO")
.setValue(this.plugin.settings.filenamePattern)
.onChange(async (value) => {
this.plugin.settings.filenamePattern = value;
await this.plugin.saveSettings();
}),
);
}
}