-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.ts
181 lines (156 loc) · 4.8 KB
/
main.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
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
import {
App,
Notice,
Plugin,
PluginSettingTab,
Setting,
debounce,
TFile,
} from "obsidian";
import type moment from "moment";
const DEFAULT_SETTINGS: ChangelogSettings = {
numberOfFilesToShow: 10,
changelogFilePath: "",
watchVaultChange: false,
};
declare global {
interface Window {
app: App;
moment: typeof moment;
}
}
export default class Changelog extends Plugin {
settings: ChangelogSettings;
async onload() {
console.log("Loading Changelog plugin");
await this.loadSettings();
this.addSettingTab(new ChangelogSettingsTab(this.app, this));
this.addCommand({
id: "update",
name: "update",
callback: () => this.writeChangelog(),
hotkeys: [],
});
this.watchVaultChange = debounce(
this.watchVaultChange.bind(this),
200,
false
);
this.registerWatchVaultEvents();
}
registerWatchVaultEvents() {
if (this.settings.watchVaultChange) {
this.registerEvent(this.app.vault.on("modify", this.watchVaultChange));
this.registerEvent(this.app.vault.on("delete", this.watchVaultChange));
this.registerEvent(this.app.vault.on("rename", this.watchVaultChange));
} else {
this.app.vault.off("modify", this.watchVaultChange);
this.app.vault.off("delete", this.watchVaultChange);
this.app.vault.off("rename", this.watchVaultChange);
}
}
watchVaultChange(file: any) {
if (file.path === this.settings.changelogFilePath) {
return;
} else {
this.writeChangelog();
}
}
async writeChangelog() {
const changelog = this.buildChangelog();
await this.writeInFile(this.settings.changelogFilePath, changelog);
}
buildChangelog(): string {
const files = this.app.vault.getMarkdownFiles();
const recentlyEditedFiles = files
// Remove changelog file from recentlyEditedFiles list
.filter(
(recentlyEditedFile) =>
recentlyEditedFile.path !== this.settings.changelogFilePath
)
.sort((a, b) => (a.stat.mtime < b.stat.mtime ? 1 : -1))
.slice(0, this.settings.numberOfFilesToShow);
let changelogContent = ``;
for (let recentlyEditedFile of recentlyEditedFiles) {
// TODO: make date format configurable (and validate it)
const humanTime = window
.moment(recentlyEditedFile.stat.mtime)
.format("YYYY-MM-DD [at] HH[h]mm");
changelogContent += `- ${humanTime} · [[${recentlyEditedFile.basename}]]\n`;
}
return changelogContent;
}
async writeInFile(filePath: string, content: string) {
const file = this.app.vault.getAbstractFileByPath(filePath);
if (file instanceof TFile) {
await this.app.vault.modify(file, content);
} else {
new Notice("Couldn't write changelog: check the file path");
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
onunload() {
console.log("Unloading Changelog plugin");
}
}
interface ChangelogSettings {
changelogFilePath: string;
numberOfFilesToShow: number;
watchVaultChange: boolean;
}
class ChangelogSettingsTab extends PluginSettingTab {
plugin: Changelog;
constructor(app: App, plugin: Changelog) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
const settings = this.plugin.settings;
new Setting(containerEl)
.setName("Changelog note location")
.setDesc("Changelog file absolute path (including the extension)")
.addText((text) => {
text
.setPlaceholder("Example: Folder/Changelog.md")
.setValue(settings.changelogFilePath)
.onChange((value) => {
settings.changelogFilePath = value;
this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Number of recent files in changelog")
.setDesc("Number of most recently edited files to show in the changelog")
.addText((text) =>
text
.setValue(String(settings.numberOfFilesToShow))
.onChange((value) => {
if (!isNaN(Number(value))) {
settings.numberOfFilesToShow = Number(value);
this.plugin.saveSettings();
}
})
);
new Setting(containerEl)
.setName("Automatically update changelog")
.setDesc(
"Automatically update changelog on any vault change (modification, renaming or deletion of a note)"
)
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.watchVaultChange)
.onChange((value) => {
this.plugin.settings.watchVaultChange = value;
this.plugin.saveSettings();
this.plugin.registerWatchVaultEvents();
})
);
}
}