Show current writing progression using frontmatter variable #531
SerafinDinges
started this conversation in
Templates Showcase
Replies: 1 comment
-
adapted the snippet to also respect a <%*
function characterGoal(tp) {
// read variable from frontmatter
let characterGoal = false;
let finishBy = false;
try {
characterGoal = tp.file.content
.split("\n")
.filter((line) => line.includes("characterGoal: "))[0]
.split(": ")[1];
} catch (error) {
if (!characterGoal) {
new Notice("no characterGoal frontmatter variable");
return "";
}
}
try {
finishBy = tp.file.content
.split("\n")
.filter((line) => line.includes("finishBy: "))[0]
.split(": ")[1];
} catch (error) {
if (!finishBy) {
new Notice("no finishBy frontmatter variable");
return "";
}
}
finishBy = moment(finishBy);
let current = moment().startOf("day");
let daysLeft = calculateBusinessDays(finishBy, current);
let charactersDone = tp.file.content.length;
let charactersLeft = characterGoal - charactersDone;
let dailyGoal = (charactersLeft / daysLeft).toFixed(0);
let percentage = ((charactersDone / characterGoal) * 100).toFixed(0);
let notice = `Progression
${tp.file.content.length} of ${characterGoal}
${percentage}%
${daysLeft} weekdays left
Daily goal: ${dailyGoal} characters`;
new Notice(notice);
return "";
}
function calculateBusinessDays(firstDate, secondDate) {
let day1 = moment(firstDate).startOf("day");
let day2 = moment(secondDate).startOf("day");
let adjust = 1;
if (day1.dayOfYear() === day2.dayOfYear() && day1.year() === day2.year()) {
return 0;
}
if (day2.isBefore(day1)) {
const temp = day1;
day1 = day2;
day2 = temp;
}
if (day1.day() === 6) {
day1.day(8);
} else if (day1.day() === 0) {
day1.day(1);
}
if (day2.day() === 6) {
day2.day(5);
} else if (day2.day() === 0) {
day2.day(-2);
}
const day1Week = day1.week();
let day2Week = day2.week();
if (day1Week !== day2Week) {
if (day2Week < day1Week) {
day2Week += day1Week;
}
adjust += -2 * (day2Week - day1Week);
}
return day2.diff(day1, "days") + adjust;
}
characterGoal(tp);
%> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Just wrote a quick snippet that shows a notice about the current progress of reaching my writing goal.
I'm currently writing my master's thesis and needed something like this. I've added a hotkey for the template, so now every time I trigger that it shows me the current percentage. The percentage is calculated using a frontmatter variable called
characterGoal
. So for it to work you need to put something like this at the top of the document.This is currently quite hacky but figured I'd share it anyway as it's pretty useful to me and I couldn't really find anything comparable.
It doesn't actually add any content to the current document but reappropriates the Templates plugin to just execute arbitrary JavaScript. (Which by the way is just amazing. I only recently discovered Obsidian and I'm so blown away by the fact that all this exists!)
Let me know if you have any questions or improvements or other ways to do this.
Beta Was this translation helpful? Give feedback.
All reactions