Skip to content

Instantly share code, notes, and snippets.

@drmikecrowe
Last active February 1, 2024 18:02
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 drmikecrowe/f3e38008562791cc3449f482bbbbf717 to your computer and use it in GitHub Desktop.
Save drmikecrowe/f3e38008562791cc3449f482bbbbf717 to your computer and use it in GitHub Desktop.
My journaling template

I created the journals object that links a tag to a nested folder for all my journals. There are multiple issues this solves:

  • Journaling across multiple clients in different folders
  • Clicking on a link to a new journal entry that doesn't exist (see breadcrumb nested folder)
  • Client tagging

In the previous/next links, my link format is:

<< [[<% tag %>-<% tp.date.now("YYYY-MM-DD", -1) %>]] | [[<% tag %>-<% tp.date.now("YYYY-MM-DD", 1) %>]] >>

There are 3 possibilities you might trigger:

  1. Clicking the Next link and it doesn't exist, which creates a new Untitled note in the root folder
  2. Right clicking on a Journal folder and creating a New Note
  3. Automatic daily note in a folder (really a variant of #2 above)

The critical solution here is to determine if you need to move the note or rename it. Choosing the wrong one results in duplicate notes.

To solve for #1, a new note at the root folder must be moved:

const dir = tp.file.folder(true);
const isAtRoot = dir === '/';
...
if (title.startsWith(`${tag}-${year}`) && isAtRoot) {
...

To solve for #2, the new Untitled note in the correct folder must be renamed:

if (dir.startsWith(folder) && title === 'Untitled')
...

Additionally, this allowed me to customize the template for each journal. For instance, you can see where I do:

<%* if (tag === 'personal') { %>

Weight: #weight 
<%* } %>

to customize the template for my personal journal. This can also be done for each client as well if needed.

Here's the template:

Template:

---
aliases: ["<% tp.date.now("MMMM Do, YYYY", 0) %>","<% tp.date.now("MMMM D, YYYY", 0) %>","<% tp.date.now("MMM D, YYYY", 0) %>","<% tp.date.now("MMM. D, YYYY", 0) %>","<% tp.date.now("M/D/YYYY", 0) %>","<% tp.date.now("M-D-YYYY", 0) %>","<% tp.date.now("YYYY-MM-DD", 0) %>","<% tp.date.now("M.D.YYYY", 0) %>"]
created: ["<% tp.file.creation_date("YYYY.MM.DD h:mm A") %>"]
---
<%*
const testJournal = t => t.toLowerCase().indexOf("journal") > -1;
const journals = {
	personal: "05-Journal",
	client1: "02-🏦 Work/Client1/Journal",
	client2: "02-🏦 Work/Client2/Journal"
};

const title = tp.file.title;
const dir = tp.file.folder(true);
const isAtRoot = dir === '/';

let isJournal = testJournal(title)

const year = tp.date.now("YYYY");
const month = tp.date.now("YYYY-MM");
const day = tp.date.now("YYYY-MM-DD");

console.log(`title=${title}, dir=${dir}, isJournal=${isJournal}`);

for ([tag, folder] of Object.entries(journals)) {
	const journalname = `${tag}-${day}`;
	const fullname = `${folder}/${year}/${month}/${journalname}`;
	// Here we clicked on the next link and the new note is at the root folder
	// Need to move it into the right folder
	if (title.startsWith(`${tag}-${year}`) && isAtRoot) {
		console.log(`Looks like title starts with ${tag}, so I need to move it into ${fullname}`);
		await tp.file.move(fullname);
		isJournal = testJournal(fullname);
		break;
	// Here we either right-clicked and did a new note in the correct folder or a daily note triggered.  
	// Need to rename and not move
	} else if (dir.startsWith(folder) && title === 'Untitled') {
		console.log(`Looks like this is a new file in ${dir}, rename to ${journalname}`);
		isJournal = testJournal(folder);
		await tp.file.rename(journalname);
		break;
	}
}
%>
<%* if (isJournal) { %>
#<% tag %>

## Journal:
- <%* tp.file.cursor() %>
<%* } %>
<%* if (tag === 'personal') { %>

Weight: #weight 
<%* } %>

<< [[<% tag %>-<% tp.date.now("YYYY-MM-DD", -1) %>]] | [[<% tag %>-<% tp.date.now("YYYY-MM-DD", 1) %>]] >>

Final result: 2022-03-19_08-06

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