Templater Plugin

up:: Obsidian Plugins

Resources

Snippets

Prompt

var retVal = await tp.system.prompt("Enter name : ");
document.write("You have entered : " + retVal);

Chooser

const choicesToChoose = await tp.system.suggester(["array", "for", "display"], ["return", "array", "yay"]);

Notify

new Notice("Hello world!");

Create file

tp.file.create_new(content, title, false);

Update content

// Replace content
const content = tp.file.content;
var replaced = content.replace("status: to-read", "status: currently-reading")
 
// Write content
const tFile = tp.file.find_tfile(tp.file.title);
const newContent = replaced;
await app.vault.modify(tFile, replaced);

Find/Replace Function

async function findReplace(find, replace) {
// Replace content
const content = tp.file.content;
var replaced = content.replace(find, replace)
 
// Write content
const tFile = tp.file.find_tfile(tp.file.title);
const newContent = replaced;
await app.vault.modify(tFile, newContent);
}

Move file

const targetFolder = "Example";
if (tp.file.folder != targetFolder) { 
await tp.file.move('/' + targetFolder + '/' + tp.file.title)
}
 

Append content

// append content
const filename = tp.date.now("YYYY-MM-DD")
const append = `* 📚Read [[${tp.file.title}]]`
 
const originalContent = await app.vault.read(tp.file.find_tfile(filename));
const contentNew = `${originalContent} ${append}`
await app.vault.modify(file, contentNew) 

Appending to Dailies

// Append to Daily
var dailyDate = "DATE"; //date of the daily note to append to/create (eg."2022-05-20" or 'tp.file.now("YYYY-MM-DD")'
const append = `* TEXT`; //text to append
 
const folder = app.vault.getAbstractFileByPath("20 Journals");
if (!tp.file.exists(dailyDate)) {
    await tp.file.create_new(append,dailyDate, false, folder)
new Notice('🆕Created new Daily!');
} else {
    const originalContent = await app.vault.read(tp.file.find_tfile(dailyDate));
    const contentNew = `${originalContent}\n${append}`
    await app.vault.modify(tp.file.find_tfile(dailyDate), contentNew)
new Notice('📌Appended to Daily!');
}

Append with template

// Append to file
//------------------
// Edit this
var displayName = "Weekly";
var appendFile = "filename";
const append = "string";
var template = "12 Weekly Template";
var folder = "20 Journals"; //of template file
//------------------
// Set vars
template = tp.file.find_tfile(template);
folder = app.vault.getAbstractFileByPath(folder);
// Create if non-existent
if (!tp.file.exists(appendFile)) {
    await tp.file.create_new(template, appendFile, false, folder)
    new Notice(`🆕Created new ${displayName}!`);
}
// Append to file
const originalContent = await app.vault.read(tp.file.find_tfile(appendFile));
const contentNew = `${originalContent}\n${append}`
await app.vault.modify(tp.file.find_tfile(appendFile), contentNew)
new Notice(`📌Appended to ${displayName}`);

Add to file

// Add to Kanban
const file = tp.file.find_tfile("Books Kanban");
const newContent = "\n- [ ] [[" + tp.file.title + "]]";
const originalContent = await app.vault.read(file);
const content = `${originalContent} ${newContent}`
await app.vault.modify(file, content)