Can't replace note contents with templater

First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.

What I’m trying to do

I have some code which reads the current file, modifies the content and does a full replacement of the note contents. It used to work, but I haven’t used it for a while and now it seems to be broke.

Things I have tried

I’ve done quite a bit of debugging and it appears that the problem is when I try to replace the contents of the note. All that ends up happening is that a couple of CRs get written wherever the cursor is (instead of replacing the entire note contents). Here’s my latest test code:

<%*
function replaceNoteText(str) {
	console.log(str)
	console.log("Ready to write")
	const file = app.workspace.activeLeaf.view.file; // *get current file*
	//const file = app.workspace.getActiveFile; // *get current file*
	console.log("File: " + file)
	app.vault.modify(file, str); // *replace content with new content*
	console.log("finished modify")
}

replaceNoteText("this is junk")

-%>

It appears the modify is not replacing the file and simply inserting a couple of CRs (or LFs)

If i run this, the console log is:
[Violation] Forced reflow while executing JavaScript took 31ms
plugin:obsidian-annotator:6 [Violation] ‘setInterval’ handler took 50ms
VM384:7 this is junk
VM384:8 Ready to write
VM384:11 File: [object Object]
VM384:13 finished modify
app.js:1 [Violation] ‘setTimeout’ handler took 68ms

Can someone tell me what I’m doing wrong?

For starters, anytime you’re reading or writing to files you should use await in front of that call. In this case you should do await app.vault.modify( ... ).

This might trigger the need to make that function async, and afterwards, that you need to do await replaceNoteText( ... ) as well.

Doing both of these you’ll end up with:

<%*
async function replaceNoteText(str) {
	console.log(str)
	console.log("Ready to write")
	const file = app.workspace.activeLeaf.view.file; // *get current file*
	//const file = app.workspace.getActiveFile; // *get current file*
	console.log("File: " + file)
	await app.vault.modify(file, str); // *replace content with new content*
	console.log("finished modify")
}

await replaceNoteText("this is junk")

-%>

Try that, and see if that resolve your issues. If not, you’ll need to investigate into what those Violation messages indicate.

Update: Here a stack overflow thread on that message: google chrome - Violation Long running JavaScript task took xx ms - Stack Overflow . And it seems to be related to changing the DOM when it’s not expecting it, aka a typical async/await issue as I’ve already provided a fix for.

That seemed to fix it. I understand why I need the async/await. I don’t understand why it has worked before and not now (though I probably won’t lose much sleep figuring that out).

Thanks for you help.

Scott

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.