How do I add content to a file after it has ran JavaScript code? My JavaScript code prompts the user for a title and for some tags using a multi-suggester. The code then uses the inputted tags to build the frontmatter dynamically.
<%*
const title = await tp.system.prompt("Enter company title: ");
await tp.file.rename(title);
const selectedTags = await tp.system.multi_suggester(['july-14', 'july-15', 'july-16'], ['july-14', 'july-15', 'july-16'], false, "Select the days the company will be there:");
const file = tp.file.find_tfile(title);
await tp.app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter['base'] = "[[Companies to talk to.base]]";
frontmatter['tags'] = frontmatter["tags"] || [];
frontmatter['tags'].push(...selectedTags);
});
%>
Things I have tried
- Plainly adding the content before and right after the JavaScript code
### Description
<%*
const title = await tp.system.prompt("Enter company title: ");
await tp.file.rename(title);
const selectedTags = await tp.system.multi_suggester(['july-14', 'july-15', 'july-16'], ['july-14', 'july-15', 'july-16'], false, "Select the days the company will be there:");
const file = tp.file.find_tfile(title);
await tp.app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter['base'] = "[[Companies to talk to.base]]";
frontmatter['tags'] = frontmatter["tags"] || [];
frontmatter['tags'].push(...selectedTags);
});
%>
### More headers here
- Displaying it
<%*
const title = await tp.system.prompt("Enter company title: ");
await tp.file.rename(title);
const selectedTags = await tp.system.multi_suggester(['july-14', 'july-15', 'july-16'], ['july-14', 'july-15', 'july-16'], false, "Select the days the company will be there:");
const file = tp.file.find_tfile(title);
await tp.app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter['base'] = "[[Companies to talk to.base]]";
frontmatter['tags'] = frontmatter["tags"] || [];
frontmatter['tags'].push(...selectedTags);
});
const desc = "### Description";
%>
<% desc %>
- Using app.vault.modify
<%*
const title = await tp.system.prompt("Enter company title: ");
await tp.file.rename(title);
const selectedTags = await tp.system.multi_suggester(['july-14', 'july-15', 'july-16'], ['july-14', 'july-15', 'july-16'], false, "Select the days the company will be there:");
const file = tp.file.find_tfile(title);
await tp.app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter['base'] = "[[Companies to talk to.base]]";
frontmatter['tags'] = frontmatter["tags"] || [];
frontmatter['tags'].push(...selectedTags);
});
const desc = "### Description";
await app.vault.modify(file, tp.file.content + desc);
%>
In all of the things I’ve tried that are listed above, I’ve tried both having the code as is, and inside the callback function of the on_all_templates_executed hook and still no luck. If it matters, I have my Templater settings set up in a way that if I create a new file inside the specified folder, it automatically applies the template. I don’t create a new file and manually select the “Templater: Open insert template modal” option from the Command Palette. Any advice?
Additionally, when should I use the on_all_templates_executed hook? I understand that whatever is inside the callback function runs after all of the templates have finished executing. I do have another, separate template that has the same code in adding the tags to the frontmatter dynamically THAT’S NOT inside the callback function of the hook, yet it still works. Why is that?