Skip to content

Commit

Permalink
Update Insert Modified Notes to Daily Notes.md
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LeCheenaX authored Dec 28, 2024
1 parent f10f4de commit 3d7768b
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions Templates/Insert Modified Notes to Daily Notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ const dv = app.plugins.plugins["dataview"].api;
let today = moment().format("YYYY-MM-DD");
let DailyNote = moment(today).format("YYYY-MM-DD");
let recordNote = DailyNote;
let note = app.vault.getAbstractFileByPath(`${RECORD_NOTE_FOLDER}/${recordNote}.md`);

// Delay function
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

new Notice("Autoupdate scripts are running! ", 3000);
console.log("Autoupdate scripts are running! ");
console.log("[Modified File Recorder] Autoupdate scripts are running! ");

// Function to create a new note
async function createNewNote() {
await tp.file.create_new("", recordNote, false, RECORD_NOTE_FOLDER);
new Notice(`Created new note ${recordNote} in folder ${RECORD_NOTE_FOLDER}.`, 5000);
console.log(`Created new note ${recordNote} in folder ${RECORD_NOTE_FOLDER}.`);
await delay(500);
console.log(`[Modified File Recorder] Created new note ${recordNote} in folder ${RECORD_NOTE_FOLDER}.`);
await delay(2000);
}

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

// Function to read note content
async function readDailyNoteContent() {
async function readDailyNoteContent(note) {
return await app.vault.read(note);
}

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

Expand All @@ -70,14 +69,15 @@ async function updateDailyNotes() {
if (!tp.file.find_tfile(recordNote)) {
await createNewNote();
}


let note = app.vault.getAbstractFileByPath(`${RECORD_NOTE_FOLDER}/${recordNote}.md`);
const dvqueryOutput = await fetchQueryOutput();
const recordData = processQueryOutput(dvqueryOutput.value);
const content = await readDailyNoteContent();
await updateNoteContent(content, recordData);
const content = await readDailyNoteContent(note);
await updateNoteContent(content, recordData, note);
} catch (error) {
new Notice("⚠️ An unexpected error occurred: " + error.message, 5000);
console.log(`⚠️ ERROR: ${error}`);
console.log(`[Modified File Recorder] ⚠️ An unexpected error occurred: ${error}`);
}
}

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

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

Expand Down

0 comments on commit 3d7768b

Please sign in to comment.