Skip to content

Instantly share code, notes, and snippets.

@torantine
Last active June 4, 2021 19:45
Show Gist options
  • Save torantine/5cdb963c65fed363564648b294e498d1 to your computer and use it in GitHub Desktop.
Save torantine/5cdb963c65fed363564648b294e498d1 to your computer and use it in GitHub Desktop.
This templater template will create a years worth of files from the year you want in the folder you want. Copy the raw text into an md file in Obsidian.
<%*
// cleaner version can be made using moment.js
// format date to YYYY-MM-DD (date -> string)
function formatDate(date) {
// https://stackoverflow.com/questions/23593052/format-javascript-date-as-yyyy-mm-dd
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
// get year to make files for and check if leap year
let year = (await tp.system.prompt("year?", "2021", 1))
let daysInYear;
let leapYear = parseInt(year)%4 == 0 ? daysInYear = 366 : daysInYear = 365;
// get files and folders in vault
let faf = this.app.vault.fileMap;
let fafNames = Object.keys(this.app.vault.fileMap);
// for each file and folder, check type then send to proper array
let folders = [];
let files = [];
let pushFilesAndFolders = (element) => {
element.includes(".md") ? files.push(element) : folders.push(element+"/");
}
fafNames.forEach(pushFilesAndFolders)
folders.unshift("-----Pick folder to place files in-----") // add instruction to userFolder suggestion
// get folder to make files in
let userFolder;
while(userFolder == null || userFolder.length < 1 || userFolder.contains("-----Pick folder to place files in-----")){
userFolder = (await tp.system.suggester(
((item) => item),
folders, 1))
}
// make array with names of files to create
let start = "01/01/"+year;
let end = "12/31/"+year;
let date = new Date(start);
let fileNames = [];
let getFileNames = () => {
for(let i = 0; i<daysInYear;i++){
fileNames.push(formatDate(date));
date.setDate(date.getDate() +1)
}
}
getFileNames()
let confirm = (await tp.system.suggester(
((item) => item),
["ARE YOU SURE...CLICK ME (OTHERWISE HIT ESC)"], 1))
for(let i = 0;i<fileNames.length;i++){
let filePath = userFolder + fileNames[i] + '.md';
let userFile = this.app.vault.getAbstractFileByPath(filePath);
if(!userFile) {
userFile = await this.app.vault.create(filePath, '');
}
}
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment