It took a while to figure out the approach, so I think it’s shareworthy.
It is an attempt to have a meta template (sorry for the buzzword):
-
one flow for all notes created (either thru a non-existing link, with a shortcut, or generated by other plugins where possible):
- nesting of templates is possible
- moving of notes is possible
- renaming of notes is possible
-
apply specific templates based on note name (initially I wanted to be as independent of a note’s name, eventually, that became a trade-off to be in line with point 1):
- to be able to choose the template per note name or metadata
- folder templates in plugin settings hadn’t worked with regular expression or date placeholder
- to be able to generate ZK unique ID for note naming convention (optional, a matter of personal preference)
Here is how it looks thus far:
<%*
let templates = {};
const fileTitle = tp.file.title;
//tp.file.day will be appreciated here
const fileDate = moment(fileTitle);
if (isNaN(fileDate.toDate()) == false) {
const keys = fileDate.format("YYYYMMDDHHmm,YYYY-MM-DD,GGGG-[W]WW,YYYY-MM,YYYY-[Q]Q,YYYY").split(',');
const values = "zettelid,daily,weekly,monthly,quarterly,yearly".split(',');
keys.forEach((key,i) => templates[key] = values[i]);
} else {
templates[fileTitle] = "zettelid";
}
const templatePath = "[" + "[" + "metadata/templates/journal-" + templates[fileTitle] + "-template" + "]" + "]";
tR += await tp.file.include(templatePath);
-%>
The only part I miss is to make ZK UID work with Note compose or Note refactor.
I’m no expert with JS, so feel free to beautify, if you see a more laconic way (esp. the part with Obsidian link, most of the samples with tp.file.include
refer to a static wiki link, so I experimented with dynamic template path)
TL;DR
I have several templates and want them to fire based on the file name (per date or regexp). I ended up with the main template to rule ‘em all. Tried other ways beforehand and haven’t found the options’ combination to cover all desired points