About daily journaling templates and dycamically generated date links

What I’m trying to do

I have this template:

#### << [[<%tp.date.now("YY-MM-DD", -1)%>]] | [[journal]] | [[<% tp.date.now("YY-MM-DD", 1) %>]] >>
---
<p align="right"><b><font size="+1"><%tp.date.now("MMM Do, YYYY").toLowerCase()%></font><br><font size="+1"><%tp.date.now("h:mm a") %></font></b></p>
text goes here


---
<p align="right"><font size="+1"><b>this day six months ago</b></font></p>

![[<% tp.date.now("YY-MM-DD", "P-6M") %>]]

---

It automatically generates my daily journal page, and creates a few pretty links to the previous day, next day, 6 months ago and my index of journal pages. So far so good. I do not really journal everyday, so I was wondering if I could use data queries to dynamically search and show the next existing file instead of the next day.

Things I have tried

I tried using the dataview plugin, and to some extend I got what I wanted. I can fetch the next and the previous entry based on data creation.

list
FROM "journal" 
WHERE file.ctime < this.file.ctime AND file.name != "journal"
sort file.mtime DESCENDING
limit 1
list
FROM "journal" 
WHERE file.ctime > this.file.ctime AND file.name != "journal"
sort file.mtime ASCENDING
limit 1

But I could not find a way to put that specific data entry in my title, or in any way that it wouldn’t look practical and good in the template. Can you guys suggest a way of having those links dynamically on a more minimal or aesthetical way?

Thank you!

Use date: ... in your files

If you put a property called date with a proper date, you’ll be able to use file.day from within dataview and various other plugins. Then you’re free to do whatever you want with the title, and still make your queries be relative to the date of the note.

Possibly use dv.view()

In my daily note setup I’m also using something similar to what you’re aiming for, so my template inserts the following as the first line in my daily notes (and some other notes):

`$= await dv.view("js/header") `

This loads and executes the stuff from within js/header.js. Doing it this allows for me to change the header whenever I feel like it, and all past (and new) daily notes will reflect those changes immediately. Using queries like yours, could very easily be outdated at some point, leaving you with a rather large search-and-replace operation.

However this solution does require a little bit of javascript knowledge, but it also allows for a lot of stuff to be done behind the scenes.

Better date handling in a template

Finally if you start of your template with the following:

---
<%* const today = tp.date.now(""YY-MM-DD") _%>
date: <% tp.date.now("YYYY-MM-DD", 0, today, "YY-MM-DD") %>
... other frontmatter stuff ...
---

You could later on do stuff like:

Yesterday: [[<% tp.date.now("YY-MM-DD", -1, today, "YY-MM-DD") %>]]

Six months ago: ![[% tp.date.now("YY-MM-DD", "P-6M", today, "YY-MM-DD") %>]]

This way you use today as a reference date throughout your template, and you ensure that in any scripts you can do file.day to refer to this date. (If you switch to using the YYYY-MM-DD format for the reference date in the first line, the latter variants could be like tp.date.now("YY-MM-DD", "P-6M", today) and leave out that format string. )

Hi! Right off the bat, that looks like an amazing way (and better than mine) of handling dates. Thank you for your suggestions, and while Js is a bit daunting to me, I am messing around and discovering how to properly make that work.

But unfortunately, that does not track the original issue of displaying the previously existing file in a way that looks good in the template, unless I did not understand how what you said could apply to the issue. But that is on me, I think I did not express myself well.

Right now, once I create a daily file, I get something that looks like this:

#### << [[<%tp.date.now("YY-MM-DD", -1)%>]] | [[journal]] | [[<% tp.date.now("YY-MM-DD", 1) %>]] >>

Result:

I was wondering if instead of fetching the yesterday and next day, it would be possible to put the previous existing page and next existing page inside the markdown link. Right now, with that query, I can get that information in a list or table view, but not in a text. For example, as of now, I have settled for a table view of the discussed query before the file, since that is what the dataview plugin allow. It looks like this:

So, I am still wondering it would be possible to, instead of displaying yesterday and tomorrow, to display the most recent previous journal entry and most recent next file.

HeHe… This was the reasoning I chose to answer your question, and I somehow forgot to address this particular variant in my ramblings. The header you’re talking about is produce by a doNav() function within my header.js file, which is included in the template as described in the previous post.

So stick the following into your header.js, and let the magic unfold:

const curr = dv.current()
const currFile = curr.file
const currFolder = curr.file.folder

let pages = await dv.pages()
  .where( p => p.file.day )
  .sort( p => p.file.day ) 

// Loop through all the pages, and select the one _before_ and _after_ current file
let thisPrevious = null
let thisNext = null
let previous = null

for (let page of pages) {
	if (previous && (page.file.path === currFile.path) ) {
		thisPrevious = previous.file
        // console.log("Previous found", previous)
	} 

	if (previous && (previous.file.path === currFile.path) ) {
		thisNext = page.file
		break
	}

	previous = page 
}

// Output stuff
const linkSection = [
    thisPrevious ? thisPrevious.link : "None before" ,
    `[[${ currFile.path }|journal]]`,     
    thisNext ? thisNext.link : "None after"
  ].join(' | ')
  
dv.span(`<< ${ linkSection } >>`)

In my test setup this outputs as:
image

The approach to get the previous and next files are related to the let pages = ... lines, which builds a list of all the candidates for those files, and then a little lower down, in the for (let page of pages) ... I loop through all those files keeping track of the previous file, and which is the current file. When I’ve found both candidates I break out of the loop, and present the result using dv.span() .

If you want, you could/should limit the pages query to make sure it’s only journal files. Possibly by changing the start to something like dv.pages('"journal"') where journal is your folder of daily notes. Note the combination of single and double straight quotes in that statement. The outer set is single quotes, and the inner set it double quotes.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.