Skip to content

Instantly share code, notes, and snippets.

@NISH1001
Created July 16, 2024 21:06
Show Gist options
  • Save NISH1001/cb74099a8abc80a4dcc550cc1e44e1c0 to your computer and use it in GitHub Desktop.
Save NISH1001/cb74099a8abc80a4dcc550cc1e44e1c0 to your computer and use it in GitHub Desktop.
Obsidian Templater date picker template
<%*
const fs = require('fs');
const path = require('path');
const dayOfWeek = moment().day();
const dateFormat = "YYYY-MM-DD"; // Change this if your daily notes use a different format
// Construct the path to the daily-notes.json file dynamically
const obsidianFolderPath = app.vault.adapter.basePath;
const dailyNotesConfigPath = path.join(obsidianFolderPath, '.obsidian', 'daily-notes.json');
// Read and parse the daily notes configuration file
let dailyNotesFolder = 'Daily'; // Default value in case reading the file fails
try {
const config = JSON.parse(fs.readFileSync(dailyNotesConfigPath, 'utf8'));
dailyNotesFolder = config.folder;
} catch (error) {
console.error('Failed to read daily-notes.json:', error);
}
const suggestions = new Map();
suggestions.set("manual", null);
suggestions.set("day before yesterday", moment().subtract(2, 'days'));
suggestions.set("yesterday", moment().subtract(1, 'days'));
suggestions.set("today", moment());
suggestions.set("tomorrow", moment().add(1, 'days'));
suggestions.set("next week", moment().add(1, 'weeks'));
suggestions.set("next monday", moment().add(1, 'weeks').day(1));
suggestions.set(dayOfWeek >= 2 ? "next tuesday" : "tuesday",
dayOfWeek >= 2 ? moment().add(1, 'weeks').day(2) : moment().day(2)
);
suggestions.set(dayOfWeek >= 3 ? "next wednesday" : "wednesday",
dayOfWeek >= 3 ? moment().add(1, 'weeks').day(3) : moment().day(3)
);
suggestions.set(dayOfWeek >= 4 ? "next thursday" : "thursday",
dayOfWeek >= 4 ? moment().add(1, 'weeks').day(4) : moment().day(4)
);
suggestions.set(dayOfWeek >= 5 ? "next friday" : "friday",
dayOfWeek >= 5 ? moment().add(1, 'weeks').day(5) : moment().day(5)
);
suggestions.set(dayOfWeek >= 6 ? "next saturday" : "saturday",
dayOfWeek >= 6 ? moment().add(1, 'weeks').day(6) : moment().day(6)
);
suggestions.set(dayOfWeek == 0 ? "next sunday" : "sunday",
dayOfWeek >= 0 ? moment().add(1, 'weeks').day(0) : moment().day(0)
);
suggestions.set("next year", moment().add(1, 'years'));
suggestions.set("previous year", moment().subtract(1, 'years'));
const selection = await tp.system.suggester(
[...suggestions].map(([k, v]) => k !== "manual" ? k + " (" + v.format(dateFormat) + ")" : k),
Array.from(suggestions.values())
);
let resultDate = null;
if (!selection) {
inputDate = await tp.system.prompt("Type a date (DD MM? YY?):");
resultDate = moment(inputDate, "DD MM YY");
if (!resultDate.isValid()) {
resultDate = moment();
}
} else {
resultDate = selection;
}
// Create the full path for the daily note
const dailyNotePath = `${dailyNotesFolder}/${resultDate.format(dateFormat)}`;
// Append the formatted date string with double square brackets and folder path
tp.file.cursor_append(`[[${dailyNotePath}|${resultDate.format(dateFormat)}]]`);
app.workspace.activeLeaf.view.editor.focus();
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment