-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
139 lines (112 loc) · 3.9 KB
/
main.ts
File metadata and controls
139 lines (112 loc) · 3.9 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
import { App, Notice, Plugin, PluginSettingTab, Setting, TFile} from 'obsidian';
interface WeeklyNoteSettings {
templatePath: string;
logsDir: string;
}
const DEFAULT_SETTINGS: WeeklyNoteSettings = {
templatePath: "",
logsDir: "",
}
export default class WeeklyNotePlugin extends Plugin {
settings: WeeklyNoteSettings;
getFileByPath(path: string): TFile {
return this.app.vault.getFiles().find((file) => {return file.path == path})
}
//File is named for month.numbered-week.year
getFileName(d: Date): string {
//how to get numbered week (if there is a month change
//mid week we consider this week part of the last month)
//find date of this weeks monday, count number of weeks.
let MONDAY = 1
while (d.getDay() != MONDAY) {
d.setDate(d.getDate() - 1)
}
let week = Math.floor(d.getDate()/7) + 1
let month = d.getMonth() + 1
let year = d.getFullYear()
return this.settings.logsDir + month + "." + week + "." + year + ".md"
}
async getUnfinishedTodos(fileName: string): Promise<string[]> {
let lastWeekFile = this.getFileByPath(fileName)
if (lastWeekFile == undefined) {
new Notice("couldn't find last weeks note, " + fileName)
return [""]
}
let lines = (await this.app.vault.read(lastWeekFile)).split(/\r?\n/)
return lines.filter((line) => {return line.includes("- [ ]")})
}
async fillTemplate(todos: string): Promise<string> {
let TODO_TEMPLATE_TAG = "{{TODO}}"
if (todos == "") {
todos = "- [ ] placeholder"
}
if (this.settings.templatePath == "") {
return todos
}
if (this.app.vault.getAbstractFileByPath(this.settings.templatePath) == null) {
new Notice("cant find template " + this.settings.templatePath)
return todos
}
let templateFile = this.getFileByPath(this.settings.templatePath)
let template = await this.app.vault.read(templateFile)
if (!template.includes(TODO_TEMPLATE_TAG)) {
new Notice("Can't find "+ TODO_TEMPLATE_TAG +" tag in template")
return template + todos
}
return template.replace(TODO_TEMPLATE_TAG, todos)
}
async onload() {
await this.loadSettings();
this.addRibbonIcon('document', 'Weekly Note', async () => {
let name = this.getFileName(new Date())
if (this.app.vault.getAbstractFileByPath(name) == null) {
//get last weeks un finished todos
let lastWeek = new Date()
lastWeek.setDate(lastWeek.getDate() - 7)
let lastweeksFileName = this.getFileName(lastWeek)
let todos = await this.getUnfinishedTodos(lastweeksFileName)
let content = await this.fillTemplate(todos.join("\n"))
await this.app.vault.create(name, content)
}
let file = this.getFileByPath(name)
await this.app.workspace.activeLeaf.openFile(file)
});
this.addSettingTab(new SettingTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SettingTab extends PluginSettingTab {
plugin: WeeklyNotePlugin;
constructor(app: App, plugin: WeeklyNotePlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Weekly Note Settings'});
new Setting(containerEl)
.setName('Weekly Note Template')
.addText(text => text
.setPlaceholder('path to template')
.setValue(this.plugin.settings.templatePath)
.onChange(async (value) => {
this.plugin.settings.templatePath = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Weekly Note Directory')
.addText(text => text
.setPlaceholder('path to dir')
.setValue(this.plugin.settings.logsDir)
.onChange(async (value) => {
this.plugin.settings.logsDir = value;
await this.plugin.saveSettings();
}));
}
}