Skip to content

Commit 3d7768b

Browse files
authored
Update Insert Modified Notes to Daily Notes.md
Improve behavior of console logging. Fix the bug that the `note` will be null if there's no daily note today, the `note` variable is no longer global variable but passed as an argument of function `updateDailyNote`. Extend waiting time if there's no daily note today, for templater plugin to auto apply template on new created daily notes.
1 parent f10f4de commit 3d7768b

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

Templates/Insert Modified Notes to Daily Notes.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ const dv = app.plugins.plugins["dataview"].api;
1010
let today = moment().format("YYYY-MM-DD");
1111
let DailyNote = moment(today).format("YYYY-MM-DD");
1212
let recordNote = DailyNote;
13-
let note = app.vault.getAbstractFileByPath(`${RECORD_NOTE_FOLDER}/${recordNote}.md`);
1413

1514
// Delay function
1615
function delay(ms) {
1716
return new Promise(resolve => setTimeout(resolve, ms));
1817
}
1918

2019
new Notice("Autoupdate scripts are running! ", 3000);
21-
console.log("Autoupdate scripts are running! ");
20+
console.log("[Modified File Recorder] Autoupdate scripts are running! ");
2221

2322
// Function to create a new note
2423
async function createNewNote() {
2524
await tp.file.create_new("", recordNote, false, RECORD_NOTE_FOLDER);
2625
new Notice(`Created new note ${recordNote} in folder ${RECORD_NOTE_FOLDER}.`, 5000);
27-
console.log(`Created new note ${recordNote} in folder ${RECORD_NOTE_FOLDER}.`);
28-
await delay(500);
26+
console.log(`[Modified File Recorder] Created new note ${recordNote} in folder ${RECORD_NOTE_FOLDER}.`);
27+
await delay(2000);
2928
}
3029

3130
// Function to fetch query output
@@ -34,7 +33,7 @@ async function fetchQueryOutput() {
3433
return await dv.queryMarkdown(QUERY_STRING);
3534
} catch (error) {
3635
new Notice("⚠️ ERROR querying data: " + error.message, 5000);
37-
console.log(`⚠️ ERROR: ${error}`);
36+
console.log(`[Modified File Recorder] ⚠️ ERROR: ${error}`);
3837
throw error; // Rethrow to handle in the calling function
3938
}
4039
}
@@ -46,21 +45,21 @@ function processQueryOutput(queryOutput) {
4645
}
4746

4847
// Function to read note content
49-
async function readDailyNoteContent() {
48+
async function readDailyNoteContent(note) {
5049
return await app.vault.read(note);
5150
}
5251

5352
// Function to update the note
54-
async function updateNoteContent(content, recordData) {
53+
async function updateNoteContent(content, recordData, note) {
5554
const regex = new RegExp(`${START_POSITION}[\\s\\S]*?(?=${END_POSITION})`);
5655
if (regex.test(content)) {
5756
const newContent = content.replace(regex, `${START_POSITION}\n${recordData}\n`);
5857
await app.vault.modify(note, newContent);
5958
new Notice("Daily note auto updated! ", 2000);
60-
console.log("Daily note auto updated! ");
59+
console.log("[Modified File Recorder] Daily note auto updated! ");
6160
} else {
6261
new Notice("⚠️ ERROR updating note: " + recordNote + "! Check console log.", 5000);
63-
console.log(`⚠️ ERROR: The given pattern "${START_POSITION} ... ${END_POSITION}" is not found in ${recordNote}! `);
62+
console.log(`[Modified File Recorder] ⚠️ ERROR: The given pattern "${START_POSITION} ... ${END_POSITION}" is not found in ${recordNote}! `);
6463
}
6564
}
6665

@@ -70,14 +69,15 @@ async function updateDailyNotes() {
7069
if (!tp.file.find_tfile(recordNote)) {
7170
await createNewNote();
7271
}
73-
72+
73+
let note = app.vault.getAbstractFileByPath(`${RECORD_NOTE_FOLDER}/${recordNote}.md`);
7474
const dvqueryOutput = await fetchQueryOutput();
7575
const recordData = processQueryOutput(dvqueryOutput.value);
76-
const content = await readDailyNoteContent();
77-
await updateNoteContent(content, recordData);
76+
const content = await readDailyNoteContent(note);
77+
await updateNoteContent(content, recordData, note);
7878
} catch (error) {
7979
new Notice("⚠️ An unexpected error occurred: " + error.message, 5000);
80-
console.log(`⚠️ ERROR: ${error}`);
80+
console.log(`[Modified File Recorder] ⚠️ An unexpected error occurred: ${error}`);
8181
}
8282
}
8383

@@ -92,12 +92,12 @@ function debounce(func, wait) {
9292

9393
// Set up event listener to run the update function on every file save with debounce
9494
app.vault.on('modify', debounce(async (file) => {
95-
console.log(`Detected File Change: ${file.name}`);
95+
console.log(`[Modified File Recorder] Detected File Change: ${file.name}`);
9696
if (file.name === `${recordNote}.md`) {
9797
await delay(200);
9898
} else {
99+
console.log(`[Modified File Recorder] Try updating ${recordNote}.md`);
99100
await updateDailyNotes();
100-
console.log(`Try updating ${recordNote}.md`);
101101
}
102102
}, 60000)); // 60 seconds debounce
103103

0 commit comments

Comments
 (0)