Creating links to previous and next existing daily notes

I’m doing a similar thing in my daily notes, where each of the notes have the following line at the start of the note:

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

This refers to a javascript file called within the dataview context, and in there I produce various headers for use in my daily notes. One of the elements it produces are previous and next links.

The algorithm I’m using to get these links is a query listing all the daily notes ordered by the date, and then I loop through them searching for the note I’m within. With some look ahead/-back code I then detect the interesting links, and breaks out of the loop when both are found.

The following code excerpt is lifted from my header script with some slight adaptions. You might need to adapt the query to match your settings. I’ve tried to match your setup.

const currFile = dv.current().file

// Fetch all notes ordered by date
const pages = await dv.pages('"Notes - Daily"')
  .sort ( p => )
// Variables used when looking for previous and next links
let thisPrevious = null
let thisNext = null
let previous = null   // Intermediate candidate for previous link

// Loop through looking for next and previous links
for (let page of pages) {

  if (previous && (page.file.path === currFile.path) ) {
    thisPrevious = previous.file

  if (previous && (previous.file.path === currFile.path) ) {
    thisNext = page.file
  // We've not found both, so update previous candidate
  // and lets loop again
  previous = page 

// Output the links
    thisPrevious ? : "No previous",,
    thisNext ? : "No next"
  ].join(" | ") )

For an ordinary day with both links it shows as this:

And if there is no next link (and similar for previous links):

If you don’t want the name of the current file included, then remove the, line from the output links section. And if you want some other output, you could use thisPrevious and thisNext to your liking.

1 Like