Skip to content

Instantly share code, notes, and snippets.

@elight
Last active August 8, 2022 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elight/157d532d8b2c1f6d12f6f1bd833215fb to your computer and use it in GitHub Desktop.
Save elight/157d532d8b2c1f6d12f6f1bd833215fb to your computer and use it in GitHub Desktop.
A tool I wrote to be able to generate a new file from a template and then ![[]] embed the new file into another file. Personally, I use this to embed different checklists into my daily dashboard so that I can track my completion of these tasks over time. But also so that I can hide the embed depnding on the time of day.
// I won't pretend that this is lovely code...
class EDate {
date = null;
vacation = false;
setVacation(v) {
this.vacation = v;
}
// HI SET THIS!!!
// vvvvvvvvvvvvvvvvvv
onVacation() {
return this.vacation;
}
// ^^^^^^^^^^^^^^^^^^
returnHome() {
this.date = moment();
}
travel(date) {
// When date is a quarter, convert to month for parsing
const pattern = /(\d{4})-Q(\d)/;
var matches = null;
if (matches = date.match(pattern)) {
date = `${matches[1]}-${Number(matches[2]) * 3}`
}
this.date = moment(date);
}
weekendDays() {
return ["Sat", "Sun"];
}
now() {
return this.date || moment();
}
startOfToday() {
return this.today();
}
today() {
return moment(this.now()).startOf('day');
}
today_formatted() {
return this.today().format("YYYY-MM-DD");
}
day() {
return this.today().format("ddd");
}
isWeekend() {
return this.weekendDays().includes(this.day());
}
isWorkday() {
return this.isWeekday() && !this.onVacation();
}
isDayOff() {
return !this.isWorkday();
}
prevDay() {
return moment(this.now()).subtract(1, 'day').format('YYYY-MM-DD');
}
nextDay() {
return moment(this.now()).add(1, 'day').format('YYYY-MM-DD');
}
tomorrow() {
return this.nextDay();
}
weekday() {
return moment(this.now()).format('ddd');
}
isWeekend() {
return this.weekday() == "Sat" || this.weekday() == "Sun";
}
isWeekday() {
return !this.isWeekend();
}
isBizday() {
return !this.isWeekend();
}
weekLink() {
return moment(this.now()).format('gggg-[W]WW');
}
week() {
return moment(this.now()).format('[W]WW');
}
prevWeek() {
return moment(this.now()).subtract(1, 'week').format('[W]WW');
}
nextWeek() {
return moment(this.now()).add(1, 'week').format('[W]WW');
}
oneWeekFromToday() {
return moment(this.now()).add(1, 'week').format('YYYY-MM-DD');
}
fourDaysFromToday() {
return moment(this.now()).add(4, 'days').format('YYYY-MM-DD');
}
prevMonth() {
return moment(this.now()).subtract(1, 'month').format('YYYY-MM');
}
nextMonth() {
return moment(this.now()).add(1, 'month').format('YYYY-MM');
}
monthLink() {
return moment(this.now()).format('YYYY-MM');
}
month() {
return moment(this.now()).format('MM');
}
quarterLink() {
return moment(this.now()).format('YYYY-[Q]Q');
}
quarter() {
return moment(this.now()).format('[Q]Q');
}
prevQuarter() {
return moment(this.now()).subtract(3, 'month').format('YYYY-[Q]Q');
}
nextQuarter() {
return moment(this.now()).add(3, 'month').format('YYYY-[Q]Q');
}
yearLink() {
return moment(this.now()).format('YYYY');
}
year() {
return this.yearLink();
}
prevYear() {
return moment(this.now()).subtract(1, 'year').format('YYYY');
}
nextYear() {
return moment(this.now()).add(1, 'year').format('YYYY');
}
}

<%* const {EDate} = customJS; const {TransclusionFromTemplate} = customJS; await TransclusionFromTemplate.run({ app: app, tp: tp, title: "Closing Out Day Checklist", targetPath: "/PeriodicNotes/Daily", template: "/Templates/new-closing-out-day-checklist", comparator: "isAfter", offset: 18, }); tR += TransclusionFromTemplate.buffer; _%>

Closing out the Day

  • #meditate at end of work day ⏯
  • Check tomorrow's schedule ⏯
  • (5m) Clean work space ⏯
  • #exercise ⏯
class TransclusionFromTemplate {
buffer;
async run(args) {
const {
tp,
app,
title,
targetPath,
template,
comparator,
offset,
} = args;
// Jot title is a Zettel timestamp and optional topic
let timestamp = tp.date.now("YYYYMMDDhhmm");
let filename = `${timestamp} ${title}`;
// Create the folder object
let folder = app.vault.getAbstractFileByPath(targetPath);
// Get the template file content
let templateFile = await tp.file.find_tfile(template);
let rawContent = await app.vault.read(templateFile);
// Create the new file in the folder from the template (do not open it by default)
await tp.file.create_new(rawContent, filename, false, folder);
let link = `![[${targetPath}/${filename}]]`;
let buffer = `\`\`\`dataviewjs
let now = moment();
// "EDate" is a small utility I wrote myself that does ugly stuff with moment.js including "time-travel"
let bound = moment('${window.customJS.EDate.today()}').add(${offset}, "hours");
if (now.${comparator}(bound)) {
dv.span('${link}');
} else {
dv.span('${link.slice(1)}');
}
\`\`\`\n`;
console.log(buffer);
this.buffer = buffer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment