Skip to content

Instantly share code, notes, and snippets.

@mnaoumov
Last active December 13, 2022 18:15
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 mnaoumov/faf6a11de7e64de31a5e5ac5a2feba53 to your computer and use it in GitHub Desktop.
Save mnaoumov/faf6a11de7e64de31a5e5ac5a2feba53 to your computer and use it in GitHub Desktop.
Obsidian Templater date creation
module.exports = async (tp, notePath, noteContent, includeDefaultHeader = true) => {
if (await tp.file.exists(notePath)) {
return;
}
const title = notePath.split('/').pop().replace('.md', '');
const nowStr = moment().format('YYYY-MM-DD-HH-mm-ssZ');
const fullNoteContent = includeDefaultHeader ? `---
title: '${title}'
created: ${nowStr}
updated: ${nowStr}
---
# ${title}
${noteContent}` : noteContent;
await tp.file.create_new(fullNoteContent, notePath, false, app.vault.getAbstractFileByPath('/'));
};
module.exports = async (tp, dateStr) => {
if (dateStr === undefined) {
dateStr = await tp.system.prompt('Enter date (YYYY-MM-DD)');
}
const date = moment(dateStr, 'YYYY-MM-DD', true);
if (!date.isValid()) {
return `!!! ${dateStr} is not a valid YYYY-MM-DD date !!!`;
}
const dateNotePath = date.format('/[Dates]/YYYY/YYYY-MM/YYYY-MM-DD[.md]');
await tp.user.createNoteIfMissing(tp, dateNotePath, `\`\`\`dataviewjs
dv.view('../../!dataview');
\`\`\`
`);
await tp.user.createNoteIfMissing(tp, date.format('/[Dates]/YYYY/YYYY-MM/YYYY-MM[.md]'), `\`\`\`dataviewjs
dv.view('../../!dataview');
\`\`\`
`);
await tp.user.createNoteIfMissing(tp, date.format('/[Dates]/YYYY/YYYY[.md]'), `\`\`\`dataviewjs
dv.view('../!dataview');
\`\`\`
`);
return app.fileManager.generateMarkdownLink(tp.file.find_tfile(dateNotePath), tp.config.target_file.path);
};

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

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