This template is a pretty nice starting point to work off of. I had some issues with “Untitled” file duplication and wanted to use it as the default, so here’s mine.
<%*
/* Prompt for note details */
let filename = await tp.system.prompt("Enter the note's filename");
const description = await tp.system.prompt("Enter the note's description");
let title = await tp.system.prompt("Enter the note's title");
/* Some input sanitation */
/* Filename RegEx sources:
* https://stackoverflow.com/a/6555220
* https://stackoverflow.com/a/28604520 */
filename = filename.replace(/\s+/g, '-').toLowerCase();
filename = filename.replace(/[^a-zA-Z0-9 -]/g, "");
/* Linter does't do input sanitation on titles when auto adding aliases to the
* front matter, so this is here to avoid value duplication after linting. */
title = title.replace(/[,]/g, "");
/* Prompt for the note's classification */
const classification = await tp.system.suggester(
["Public", "Private", "Work", "Journal", "None (Field remains blank)"],
["public", "private", "work", "journal", ""]
);
/* Prompt for note placement */
let path;
switch (classification) {
case "private":
path = "/Private/"; break;
case "work":
path = "/Work/"; break;
case "journal":
path = "/Journal/"; break;
case "public":
case "":
default:
path = await tp.system.suggester(
["root", "assets", "private", "work", "journal"],
["/", "/assets/", "/private/", "/work/", "/journal/"]
);
}
/* Saves a prefix check into a boolean and save current creation date */
let prefix;
if (classification == "journal") {
prefix = true;
} else {
prefix = await tp.system.suggester(
["(Y) Prefix date onto filename", "(N) Do not prefix date onto filename"],
[true, false]
);
}
let date;
let year;
if (classification == "journal") {
date = await tp.file.creation_date("YYYY-MM-DD");
year = await tp.file.creation_date("YYYY");
} else {
date = await tp.file.creation_date("YYYY-MM-DDTHH-mm");
}
/* Prompt for the note's license */
let license;
switch (classification) {
case "private":
case "work":
case "journal":
license = "All rights reserved"; break;
case "":
license = ""; break;
case "public":
default:
license = await tp.system.suggester(
["CC0 (Text/images)", "Unlicense (Source code)", "All rights reserved"],
["CC0", "Unlicense", "All rights reserved"]
);
}
/* Prompt for note type */
const type = await tp.system.suggester(
["Meta", "Information", "Guide"],
["meta", "information", "guide"]
);
/* Save filename plus prefix to a let variable */
let filename_date = date + "_" + filename;
let uuid = tp.user.uuid();
let uuid_short = filename + "_" + uuid.slice(0, 8);
/* If-check the prefix boolean
* NOTE: The file rename is required to avoid the creation of "Untitled" files */
if (prefix == true) {
/* Rename using filename_date and move */
await tp.file.rename(filename_date);
if (classification == "journal") {
await tp.file.move(path + year + "/" + filename_date)
} else {
await tp.file.move(path + filename_date);
}
} else {
/* Rename using filename and move */
await tp.file.rename(filename);
await tp.file.move(path + filename);
}
/* Print the multiline string as the template
* NOTE: You can use filename_date in aliases if that's wanted. */
tR = `---
title: ${title}
aliases: [${title}, ${uuid_short}, ${uuid}]
author:
description: ${description}
created: ${tp.file.creation_date("YYYY-MM-DDTHH:mm:ssZ")}
updated: ${tp.file.last_modified_date("YYYY-MM-DDTHH:mm:ssZ, [week] WW [of] MMMM [on] dddd")}
classification: ${classification}
license: ${license}
type: ${type}
tags:
views: 1
---
# ${title}
${tp.file.cursor()}`;
%>
It uses a user script file for generating uuid’s, so you’ll need to add that as well for this to work.
/* Source: https://stackoverflow.com/a/2117523 */
function uuidv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
module.exports = uuidv4;
Note that you’ll need the Linter plugin for this and have enabled Trigger Templater on new file creation
→ Enable Folder Templates
→ and set the template as the default for root. You’ll also need to enable Automatic jump to cursor
.
Also note that you’ll likely have to change some of the path names and such to fit your own vaults if you want to use this.
As an aside, while this works perfectly via Ctrl
+ N
once the correct settings have been applied, if you select the template via Alt
+ N
instead. You’ll have to use the keybind to auto-remove tp.file.cursor()
even though the cursor moves to the correct position. This is likely just a bug in Templater.