forked from badrbouslikhin/obsidian-vault-changelog
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.ts
228 lines (202 loc) · 6.34 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
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
227
228
import {
App,
Notice,
Plugin,
PluginSettingTab,
Setting,
debounce,
TFile,
} from "obsidian";
import type moment from "moment";
const DEFAULT_SETTINGS: ChangelogSettings = {
numberOfFilesToShow: 10,
changelogFilePath: "",
watchVaultChange: false,
excludePaths: "",
};
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 pathsToExclude = this.settings.excludePaths.split(',');
const cache = this.app.metadataCache;
const files = this.app.vault.getMarkdownFiles();
const recentlyEditedFiles = files
// Remove changelog file from recentlyEditedFiles list
.filter(
(recentlyEditedFile) =>
recentlyEditedFile.path !== this.settings.changelogFilePath
)
// Remove files from paths to be excluded from recentlyEditedFiles list
.filter(
function (recentlyEditedFile) {
let i;
let keep = true;
for (i = 0; i < pathsToExclude.length; i++) {
if (recentlyEditedFile.path.startsWith(pathsToExclude[i].trim())) {
keep = false;
break;
}
}
return keep;
}
)
// exclude if specifically told not to
.filter(
function (recentlyEditedFile) {
const frontMatter = cache.getFileCache(recentlyEditedFile).frontmatter;
if (frontMatter && frontMatter.publish === false) {
return false;
}
return true;
}
)
.sort((a, b) => (a.stat.mtime < b.stat.mtime ? 1 : -1))
.slice(0, this.settings.numberOfFilesToShow);
let changelogContent = ``;
let header = ``;
for (let recentlyEditedFile of recentlyEditedFiles) {
// TODO: make date format configurable (and validate it)
const humanTime = window
.moment(recentlyEditedFile.stat.mtime)
// date is already shown in the titles
.format("YYYY-MM-DD HH[h]mm");
if (header != humanTime.substring(0,10)) {
header = humanTime.substring(0,10)
changelogContent += `## ${header}\n`
}
changelogContent += `- ${humanTime.substring(10,16)} · [[${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;
excludePaths: string;
}
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();
})
);
new Setting(containerEl)
.setName("Excluded paths")
.setDesc("Paths or folders to ignore from changelog, separated by a comma")
.addText((text) => {
text
.setPlaceholder("Example: Meetings,People")
.setValue(settings.excludePaths)
.onChange((value) => {
settings.excludePaths = value;
this.plugin.saveSettings();
});
});
}
}