-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
611 lines (545 loc) · 16.4 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import {
App,
Plugin,
TFile,
Vault,
moment,
MarkdownView,
PluginSettingTab,
Setting,
} from "obsidian";
declare module "obsidian" {
interface App {
commands: {
executeCommandById(customCommand: string): unknown;
listCommands(): { id: string; name: string }[];
};
}
}
interface FrontMatterTimestampsSettings {
autoUpdate: boolean;
autoAddTimestamps: boolean;
createdPropertyName: string;
modifiedPropertyName: string;
dateFormat: string;
allowNonEmptyNewFile: boolean;
delayAddingTimestamps: number;
excludedFolders: string[];
customCommand: string;
debug: boolean;
}
const DEFAULT_SETTINGS: FrontMatterTimestampsSettings = {
autoUpdate: true,
autoAddTimestamps: true,
createdPropertyName: "created",
modifiedPropertyName: "modified",
dateFormat: "YYYY-MM-DDTHH:mm:ssZ",
allowNonEmptyNewFile: false,
delayAddingTimestamps: 1000,
excludedFolders: [],
customCommand: "",
debug: false,
};
async function calculateChecksum(file: TFile, vault: Vault): Promise<string> {
const fileContent = await vault.read(file);
const buffer = new TextEncoder().encode(fileContent);
const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
return hashHex;
}
export default class FrontMatterTimestampsPlugin extends Plugin {
settings: FrontMatterTimestampsSettings;
private lastActiveFile: TFile | null = null;
private lastChecksum: string | null = null;
private pendingNewFiles = new Set<string>();
private isPathExcluded(filePath: string): boolean {
// Immediate return if there are no excluded folders
if (this.settings.excludedFolders.length === 0) {
return false;
}
const pathSegments = filePath.split("/");
// Generate all possible subpaths to compare against excluded folders
const fullPathChecks = pathSegments.map((_, index) =>
pathSegments.slice(0, index + 1).join("/")
);
return this.settings.excludedFolders.some((excludedPath) =>
fullPathChecks.includes(excludedPath)
);
}
async onload() {
await this.loadSettings();
this.addSettingTab(new FrontMatterTimestampsSettingTab(this.app, this));
// Register the command to manually update modified time
this.addCommand({
id: "update-modified-time",
name: "Update modified time",
checkCallback: (checking: boolean) => {
const markdownView =
this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) {
if (!checking) {
this.updateModifiedTime(markdownView.file);
}
return true;
}
return false;
},
});
// Listen for new file creations
this.registerEvent(
this.app.vault.on("create", (file: TFile) => {
if (this.settings.debug) {
console.log(`File created: ${file.path}`);
}
// Mark this file as newly created so we can process it after a delay
if (
this.settings.autoAddTimestamps &&
file.extension === "md" &&
!this.isPathExcluded(file.path)
) {
this.pendingNewFiles.add(file.path);
this.handleNewFileTimestamps(file);
}
})
);
// Listen for active leaf changes if autoUpdate is enabled
if (this.settings.autoUpdate) {
this.registerEvent(
this.app.workspace.on("active-leaf-change", () =>
this.handleFileChange()
)
);
}
}
private async handleNewFileTimestamps(file: TFile) {
const { debug } = this.settings;
// If not allowed to treat non-empty new files as new, check content
if (!this.settings.allowNonEmptyNewFile) {
const fileContent = await this.app.vault.read(file);
if (fileContent.trim()) {
if (debug) {
console.log(
`File ${file.path} is not empty, skipping initial timestamps.`
);
}
this.pendingNewFiles.delete(file.path);
return;
}
}
// Wait the configured delay to allow other plugins (e.g. Templater) to do their work
if (debug) {
console.log(
`Waiting ${this.settings.delayAddingTimestamps}ms before adding timestamps to ${file.path}.`
);
}
await new Promise((resolve) =>
setTimeout(resolve, this.settings.delayAddingTimestamps)
);
// Check if file still exists after delay
if (!(await this.app.vault.adapter.exists(file.path))) {
if (debug) {
console.log(
`File ${file.path} no longer exists after delay, skipping timestamp addition.`
);
}
this.pendingNewFiles.delete(file.path);
return;
}
const currentTime = moment().format(this.settings.dateFormat);
try {
await this.app.fileManager.processFrontMatter(
file,
(frontmatter) => {
if (!frontmatter[this.settings.createdPropertyName]) {
frontmatter[this.settings.createdPropertyName] =
currentTime;
}
if (!frontmatter[this.settings.modifiedPropertyName]) {
frontmatter[this.settings.modifiedPropertyName] =
currentTime;
}
}
);
if (debug) {
console.log(`Timestamps added to new file ${file.path}`);
}
} catch (error) {
console.error(
`Error adding timestamps to new file ${file.path}`,
error
);
} finally {
this.pendingNewFiles.delete(file.path);
}
}
async handleFileChange() {
const { debug } = this.settings;
const markdownView =
this.app.workspace.getActiveViewOfType(MarkdownView);
const currentFile = markdownView ? markdownView.file : null;
if (debug) {
console.log("handleFileChange called");
}
// If no active file, check the previously active file for modifications
if (!currentFile) {
if (
this.lastActiveFile &&
!this.isPathExcluded(this.lastActiveFile.path)
) {
try {
// Check if the last active file still exists
const fileExists = await this.app.vault.adapter.exists(
this.lastActiveFile.path
);
if (fileExists) {
const currentChecksum = await calculateChecksum(
this.lastActiveFile,
this.app.vault
);
if (this.lastChecksum !== currentChecksum) {
if (debug) {
console.log(
`File ${this.lastActiveFile.path} changed while inactive, updating modified time.`
);
}
await this.updateModifiedTime(this.lastActiveFile);
}
} else if (debug) {
console.log(
`Last active file ${this.lastActiveFile.path} no longer exists.`
);
}
} catch (error) {
console.error(
`Error checking checksum for ${this.lastActiveFile.path}:`,
error
);
}
}
this.lastActiveFile = null;
this.lastChecksum = null;
return;
}
if (this.isPathExcluded(currentFile.path)) return;
// Check if switching away from another file
if (
this.lastActiveFile &&
this.lastActiveFile.path !== currentFile.path
) {
try {
const lastFileExists = await this.app.vault.adapter.exists(
this.lastActiveFile.path
);
if (lastFileExists) {
const lastFileChecksum = await calculateChecksum(
this.lastActiveFile,
this.app.vault
);
if (this.lastChecksum !== lastFileChecksum) {
if (debug) {
console.log(
`File ${this.lastActiveFile.path} changed before switching, updating modified time.`
);
}
await this.updateModifiedTime(this.lastActiveFile);
}
}
} catch (error) {
console.error(
`Error checking checksum for ${this.lastActiveFile.path}:`,
error
);
}
}
// Update the last active file and checksum if the current file has changed
if (
!this.lastActiveFile ||
this.lastActiveFile.path !== currentFile.path
) {
this.lastActiveFile = currentFile;
this.lastChecksum = await calculateChecksum(
currentFile,
this.app.vault
);
}
}
async updateModifiedTime(file: TFile | null) {
const { debug } = this.settings;
if (!file || !file.path) return;
if (this.isPathExcluded(file.path)) return;
if (file.extension !== "md") {
if (debug) {
console.log("File is not a markdown file, skipping.");
}
return;
}
const currentTime = moment().format(this.settings.dateFormat);
if (debug) {
console.log(
`Updating modified time for ${file.path} after a delay of ${this.settings.delayAddingTimestamps}ms.`
);
}
await new Promise((resolve) =>
setTimeout(resolve, this.settings.delayAddingTimestamps)
);
// Check if file still exists after delay
if (!(await this.app.vault.adapter.exists(file.path))) {
if (debug) {
console.log(
`File ${file.path} no longer exists after delay, skipping modified time update.`
);
}
return;
}
try {
await this.app.fileManager.processFrontMatter(
file,
(frontmatter) => {
frontmatter[this.settings.modifiedPropertyName] =
currentTime;
}
);
if (debug) {
console.log(`File frontmatter updated for ${file.path}`);
}
if (this.settings.customCommand) {
this.app.commands.executeCommandById(
this.settings.customCommand
);
}
} catch (error) {
console.error(
`Error updating frontmatter for file ${file.path}`,
error
);
}
}
onunload() {
console.log("Unloading Front Matter Timestamps plugin");
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class FrontMatterTimestampsSettingTab extends PluginSettingTab {
plugin: FrontMatterTimestampsPlugin;
constructor(app: App, plugin: FrontMatterTimestampsPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Automatic update")
.setDesc("Automatically update modified time on file change")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.autoUpdate)
.onChange(async (value) => {
this.plugin.settings.autoUpdate = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Automatic timestamps")
.setDesc(
"Automatically add created and modified timestamps to new notes"
)
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.autoAddTimestamps)
.onChange(async (value) => {
this.plugin.settings.autoAddTimestamps = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Created property name")
.setDesc("Customise the property name for creation timestamp")
.addText((text) =>
text
.setValue(this.plugin.settings.createdPropertyName)
.onChange(async (value) => {
this.plugin.settings.createdPropertyName = value.trim();
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Modified property name")
.setDesc("Customise the property name for modification timestamp")
.addText((text) =>
text
.setValue(this.plugin.settings.modifiedPropertyName)
.onChange(async (value) => {
this.plugin.settings.modifiedPropertyName =
value.trim();
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Date and time format")
.setDesc(
"Specify the desired date and time format using Moment.js tokens. Example: YYYY-MM-DDTHH:mm"
)
.addText((text) => {
text.setPlaceholder("YYYY-MM-DDTHH:mm:ssZ")
.setValue(this.plugin.settings.dateFormat)
.onChange(async (value) => {
this.plugin.settings.dateFormat =
value.trim() || DEFAULT_SETTINGS.dateFormat;
await this.plugin.saveSettings();
});
const resetButton = text.inputEl.parentElement!.createEl(
"button",
{
text: "Reset",
cls: "mod-ghost",
}
);
resetButton.addEventListener("click", async () => {
text.setValue(DEFAULT_SETTINGS.dateFormat);
this.plugin.settings.dateFormat =
DEFAULT_SETTINGS.dateFormat;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Allow non-empty file to be treated as new note")
.setDesc(
"Newly created file does not have to be empty to add timestamps. Enable if using plugins that automatically add content to new notes (Templater, Daily Notes, etc.)."
)
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.allowNonEmptyNewFile)
.onChange(async (value) => {
this.plugin.settings.allowNonEmptyNewFile = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Delay adding timestamps to new notes")
.setDesc(
"Delay in milliseconds before adding timestamps to new notes to avoid conflicts with other plugins that also add content to new notes. The default value of 1000 milliseconds should be sufficient for most cases, but you can adjust it as needed. Set to 0 to disable the delay if you are not experiencing any issues or not using such plugins."
)
.addText((text) =>
text
.setValue(
this.plugin.settings.delayAddingTimestamps.toString()
)
.onChange(async (value) => {
const delay = parseInt(value.trim(), 10);
if (!isNaN(delay)) {
this.plugin.settings.delayAddingTimestamps = delay;
await this.plugin.saveSettings();
}
})
);
const excludedFoldersSetting = new Setting(containerEl)
.setName("Excluded folders")
.setDesc(
"Manage folders that are excluded from timestamp updates. You can add subfolder paths as needed. For example, 'folder/subfolder' will exclude 'subfolder' but not 'folder'."
);
const listContainer = excludedFoldersSetting.settingEl.createDiv();
const updateFolderList = () => {
listContainer.empty();
this.plugin.settings.excludedFolders.forEach((folder, index) => {
const folderDiv = listContainer.createDiv("folder-entry");
folderDiv.style.display = "flex";
folderDiv.style.alignItems = "center";
folderDiv.style.justifyContent = "space-between";
folderDiv.style.marginBottom = "10px";
const folderNameSpan = folderDiv.createSpan({ text: folder });
folderNameSpan.style.flex = "1";
folderNameSpan.style.marginRight = "20px";
const removeButton = folderDiv.createEl("button", {
text: "Remove",
});
removeButton.onclick = async () => {
this.plugin.settings.excludedFolders.splice(index, 1);
await this.plugin.saveSettings();
updateFolderList();
};
});
const addButtonDiv = listContainer.createDiv();
addButtonDiv.style.display = "flex";
addButtonDiv.style.alignItems = "center";
const addInput = addButtonDiv.createEl("input", {
type: "text",
placeholder: "Add new folder path...",
});
addInput.style.flex = "1";
addInput.style.marginRight = "10px";
addInput.addEventListener("keypress", async (event) => {
if (event.key === "Enter" && addInput.value.trim().length > 0) {
const trimmedValue = addInput.value
.trim()
.replace(/\/+$/, "");
this.plugin.settings.excludedFolders.push(trimmedValue);
await this.plugin.saveSettings();
addInput.value = "";
updateFolderList();
event.preventDefault();
}
});
const addButton = addButtonDiv.createEl("button", { text: "Add" });
addButton.onclick = async () => {
if (addInput.value.trim().length > 0) {
const trimmedValue = addInput.value
.trim()
.replace(/\/+$/, "");
this.plugin.settings.excludedFolders.push(trimmedValue);
await this.plugin.saveSettings();
addInput.value = "";
updateFolderList();
}
};
};
updateFolderList();
const commandSetting = new Setting(containerEl)
.setName("Execute command after update")
.setDesc(
"Select a command to run after the modified time is successfully updated"
);
const select = commandSetting.controlEl.createEl("select");
let noneOption = select.createEl("option", { text: "None" });
noneOption.value = "";
if (this.plugin.settings.customCommand === "") {
noneOption.selected = true;
}
this.app.commands
.listCommands()
.forEach((command: { name: any; id: string }) => {
let option = select.createEl("option", { text: command.name });
option.value = command.id;
if (command.id === this.plugin.settings.customCommand) {
option.selected = true;
}
});
select.addEventListener("change", async () => {
this.plugin.settings.customCommand = select.value;
await this.plugin.saveSettings();
});
new Setting(containerEl)
.setName("Debug mode")
.setDesc("Enable debug mode to display detailed logs")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.debug)
.onChange(async (value) => {
this.plugin.settings.debug = value;
await this.plugin.saveSettings();
})
);
}
}