Files
bobbie-pkm/.trash/Template/scripts/update_front_matter_and_log.js
2026-03-02 17:06:32 +00:00

38 lines
1.5 KiB
JavaScript

// NOTE: We change the function arguments back to accept the Templater object (tp)
// This is required when running the script directly via a Templater hotkey.
async function update_front_matter_and_log(tp) {
// 1. Define timestamps (using the reliable tp.date.now)
const timestamp = tp.date.now("YYYY-MM-DDTHH:mm:ssZ");
// Get the current file object
const file = app.workspace.getActiveFile();
if (!file) {
// Must throw an error here, otherwise Templater might insert 'undefined' text.
throw new Error("No active file is open to update.");
}
// 2. Update Frontmatter Property ('updated')
await app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter['updated'] = timestamp;
});
// 3. Prepare the Log Entry Text
const logEntry = `\n- ${timestamp}: `;
// 4. Append the text to the bottom of the note body
// Using app.vault.modify is the safest way to append text at the end of a file.
await app.vault.append(file, logEntry);
// 5. Place the cursor at the end of the newly inserted text
// This allows the user to immediately type the revision summary.
// Get the current editor object
const editor = app.workspace.activeEditor.editor;
// Move cursor to the end of the file/newly added text (simulating an insertion)
editor.setCursor(editor.lastLine() + 1);
// TEMPLATER RULE: Return an empty string so nothing else gets inserted by Templater
return "";
}
module.exports = update_front_matter_and_log;