Skip to content

Instantly share code, notes, and snippets.

@lenalebt
Last active January 11, 2024 02:55
Show Gist options
  • Save lenalebt/8e58aa21392c388d8c7d65bad3206e35 to your computer and use it in GitHub Desktop.
Save lenalebt/8e58aa21392c388d8c7d65bad3206e35 to your computer and use it in GitHub Desktop.
"Rollover Daily Todos" for Obsidian Templater

Rollover Daily Todos for Obsidian, but with the Templater Plugin

Rollver daily todos is a really nice extension for Obsidian.md that takes TODOs from yesterdays daily notes and rolls them over to today's notes. It has support for Obsidians built-in templates, but does - to my understanding - not really work well with the Templater Plugin. At least, I was unable to get it to work :-). Also, I wished it had some way to tell me that TODOs have been rolled over a few times already. Doing it this way is my way of working around these limitations.

I took some of the code of that "rollover daily todos" plugin and made it into a templater user script.

How to use

  • Copy rollover_daily_todos.js into your templater user scripts directory.
  • Include it in your daily notes template, like so:
# Tasks

<% await tp.user.rollover_daily_todos() %>

If your folder id different from "Tasks/Daily", you might need to call it like this:

# Tasks

<% await tp.user.rollover_daily_todos(folder="My/Daily/Notes/Folder") %>
  • Activate templater plugin in the settings. Make sure to tick "Trigger templater on new file creation" (2), enable folder templates (3) and add your daily notes template image

  • Import the user script like this in the settings (1): image

async function getAllUnfinishedTodos(file, tasksHeader) {
const contents = await this.app.vault.read(file);
const contentsForDailyTasks = contents.split(tasksHeader)[1] || contents;
const unfinishedTodosRegex = /\t*- \[ \].*/g
const unfinishedTodos = Array.from(contentsForDailyTasks.matchAll(unfinishedTodosRegex)).map(([todo]) => todo)
return unfinishedTodos;
}
function getLastDailyNote(folder, format) {
const { moment } = window
// get all notes in directory that aren't null
const dailyNoteFiles = this.app.vault.getAllLoadedFiles()
.filter(file => file.path.startsWith(folder))
.filter(file => file.basename != null)
// remove notes that are from the future
const todayMoment = moment()
let dailyNotesTodayOrEarlier = []
dailyNoteFiles.forEach(file => {
if (moment(file.basename, format).isSameOrBefore(todayMoment, 'day')) {
dailyNotesTodayOrEarlier.push(file)
}
})
// sort by date
const sorted = dailyNotesTodayOrEarlier.sort((a, b) => moment(b.basename, format).valueOf() - moment(a.basename, format).valueOf());
return sorted[1];
};
async function rollover_daily_todos(folder = "Tasks/Daily", format = "YYYY-MM-DD", tasksHeader = "# Tasks", rolloverMarkCharacter = "❗") {
return (await getAllUnfinishedTodos(getLastDailyNote(folder, format), tasksHeader)).map(task => `${task}${rolloverMarkCharacter}`).join('\n');
}
module.exports = rollover_daily_todos;
@arbois
Copy link

arbois commented Sep 13, 2023

just a note to say thanks for an elegant solution that appears (so far) to not interfere with other plugins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment