Templater insert text at start or end of existing note regardless of current cursor position

Things I have tried

Currently I just use two templates, and add them at the top and bottom of notes, but I’d like to be able to just insert one template to cover both.

What I’m trying to do

I’d like to create a template that adds a header and a footer to an existing note, is this something that is possible?

I sometimes export notes from other software into my vault, and I would like a template that automagically adds some YAML at the start and some information at the end of the note when it’s inserted into the note. Ideally, it shouldn’t matter where the cursor is when the template is added, so it will still work if I happen to click somewhere inside the file before adding the template.

Thanks in advance.

The following template assumes you’re running it against an existing note.

<%*
const head = `---
aliases:
---

`;

const foot = `
your footer content here
`;

const tf = tp.file.find_tfile(tp.file.title);
await app.vault.modify(tf, head + tp.file.content + foot);
-%>

In my testing, the cursor ends up at the beginning of the note after adding the content. If you don’t want the cursor to move, well, that becomes a taller order which would require more thinking on my part.

1 Like

This is perfect, thanks!

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