Hey all, I wrote the following templater snippet to “promote” my notes by finding a status indicator and replacing it with the next stage status indicator.
Here is the snippet:
<%*
var file = app.workspace.getActiveFile()
var t = await app.vault.read(file)
var s = t.replace("STAGE-1", "STAGE-2").replace("STAGE-2", "STAGE-3").replace("STAGE-3", "STAGE-4")
app.vault.modify(file, s)
%>
So If I have a note that includes the text “STAGE-1”, running this template on it will change the text to “STAGE-2”. Running it again will change it to “STAGE-3”, etc. (more levels can by added by repeating .replace("STAGE-N", "STAGE-N+1") at the end).
In general, you can use this snippet for any series of text replacement operations.
In case someone else stumbles upon this, it should work backwards.
So Stage 3 should be promoted to Stage 4 before Stage 2 gets promoted to Stage 3:
<%*
var file = app.workspace.getActiveFile()
var t = await app.vault.read(file)
var s = t.replace("STAGE-3", "STAGE-4").replace("STAGE-2", "STAGE-3").replace("STAGE-1", "STAGE-2")
app.vault.modify(file, s)
%>
.replaceAll to do all instances instead of sequentially.
One more addition: When I tried this, it always added a blank line where my cursor was. Using whitespace control solved this.
So if using the example from above:
<%*
var file = app.workspace.getActiveFile()
var t = await app.vault.read(file)
var s = t.replace("STAGE-3", "STAGE-4").replace("STAGE-2", "STAGE-3").replace("STAGE-1", "STAGE-2")
app.vault.modify(file, s)
-%>
Hi this looks cool. Would something similar be used for replacing spaces (" ") in a file name with underscores and paste in the document body?
I looked at the templater document for an out of the box solution, but I don’t see one.
Thanks!