What I’m trying to do
I recently did an overhaul to the templates I am using to add metadata / frontmatter to my notes, however, the previous notes I had already created need updating.
So, I tried making a templater script (I have no clue what I am doing) to:
- Clear and update the frontmatter and tags.
- Format the note without deleting the important bits.
- Cut the content of the original note to reinsert it into itself after formatting it.
Things I have tried
There are multiple folders I wish to apply this to, for now, I started with my daily notes folder, I have searched and taken snippets of code from across multiple sources and have ended up with this:
<%*
//Links that helped me fix this annoying hurdle:
// 1 - https://zachyoung.dev/posts/templater-snippets#update-frontmatter
// 2 - https://github.com/SilentVoid13/Templater/issues/302
// 3 - https://github.com/SilentVoid13/Templater/issues/1191
// 4 - https://forum.obsidian.md/t/new-properties-and-templater-prompts-and-commands/66425/9?u=briandbecsi
// 5 - https://www.reddit.com/r/ObsidianMD/comments/1b3hjoh/how_to_push_some_text_to_another_section_within/
// 6 - https://forum.obsidian.md/t/how-do-i-replace-the-contents-of-a-file-with-templater-right-now-it-only-inserts-the-template-at-the-cursor-position/49311
// Reapply template instead of append (zachyoung)
await app.vault.modify(tp.file.find_tfile(tp.file.path(true)), "");
//## Update frontmatter (zachyoung)
//Instead of reading the contents of the file and parsing it manually, it’s recommended to use `app.fileManager.processFrontMatter` from the Obsidian public api.
//You must mutate the `frontmatter` object directly, do not try to copy the object first and then do mutations.
//## applying templater commands inside the properties' values instead of strings(Github issue forum)
// When creating a new file, we would create a race condition between Templater and the native Obsidian APIs we're using here.
// We can circumvent that with a very small delay.
setTimeout(() => { app.fileManager.processFrontMatter(this.app.workspace.getActiveFile(), (frontmatter) => {
// create a new property or update an existing one by name
//updates title property
frontmatter["title"] = `${tp.file.title}`
//updates creation date property
frontmatter["creation date"] = `${tp.file.creation_date()}`
//updates modification date property
frontmatter["modification date"] =`${tp.file.last_modified_date("dddd Do MMMM YYYY HH:mm:ss")}`
//updates file folder property
frontmatter["file folder"] = `${tp.file.folder(true)}`
//sets the tags property the following tag
frontmatter["tags"] = `dailyNote`
})
}, 1)
%>`This note was created at: <% tp.date.now("dddd Do MMMM YYYY HH:mm:ss") %>`
---
***↳*** <%*
// ## Append content in another file (zachyoung)
// Get contents of file
const file = tp.file.find_tfile(tp.file.path(true));
const content = await app.vault.read(file);
// Replace content
const newContent = content + "replace mewith me";
await app.vault.modify(file, content);
%>
---
<< Yesterday: [[<% tp.date.yesterday("YYYY-MM-DD") %> ]] | Tomorrow: [[<% tp.date.tomorrow("YYYY-MM-DD") %>]] >>
I want the content from the note that will be edited to be sectione off to be reinserted where the “↳” symbol is, but I couldn’t find a topic / discussion online with someone with this specific problem. So therefore I opened this topic here at the obsidian forum.
TLDR: I don’t know much about coding but I want to copy & paste with templater while also doing a bunch of other stuff