-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLastUpdated.js
More file actions
69 lines (56 loc) · 2.34 KB
/
LastUpdated.js
File metadata and controls
69 lines (56 loc) · 2.34 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
module.exports = () => {
// Get current date in yyyy-mm-dd format
const today = new Date();
const dateString = today.getFullYear() + '-' +
String(today.getMonth() + 1).padStart(2, '0') + '-' +
String(today.getDate()).padStart(2, '0');
// Get the active file
const activeFile = app.workspace.getActiveFile();
if (!activeFile) return;
// Read current content
app.vault.read(activeFile).then(content => {
// Check if frontmatter exists
let frontmatterEnd = 0;
let hasFrontmatter = false;
if (content.startsWith('---')) {
const secondDash = content.indexOf('---', 3);
if (secondDash !== -1) {
hasFrontmatter = true;
frontmatterEnd = secondDash + 3;
}
}
let newContent;
if (hasFrontmatter) {
// Extract existing frontmatter
const frontmatter = content.substring(0, frontmatterEnd);
const bodyContent = content.substring(frontmatterEnd);
// Check if LastReviewed already exists in frontmatter
const frontmatterContent = frontmatter.substring(3, frontmatterEnd - 3);
const lines = frontmatterContent.split('\n').filter(line => line.trim() !== '');
let lastReviewedExists = false;
for (let i = 0; i < lines.length; i++) {
if (lines[i].trim().startsWith('LastReviewed:')) {
lines[i] = `LastReviewed: ${dateString}`;
lastReviewedExists = true;
break;
}
}
if (!lastReviewedExists) {
lines.push(`LastReviewed: ${dateString}`);
}
const updatedFrontmatter = '---\n' + lines.join('\n') + '\n---';
newContent = updatedFrontmatter + bodyContent;
} else {
// No frontmatter exists, create it
const frontmatter = `---\nLastReviewed: ${dateString}\n---\n`;
newContent = frontmatter + content;
}
const gotText = content.indexOf('Last reviewed: ');
if (gotText == -1) {
newContent += "\nLast reviewed: \`INPUT[date:LastReviewed]\`\n\n"
}
// Write the updated content back to the file
app.vault.modify(activeFile, newContent);
});
return "Script completed";
}