Skip to content

Instantly share code, notes, and snippets.

@ashikahmad
Last active August 1, 2023 00:29
Show Gist options
  • Save ashikahmad/0748c4463748535cbfad2971ca5e8ee7 to your computer and use it in GitHub Desktop.
Save ashikahmad/0748c4463748535cbfad2971ca5e8ee7 to your computer and use it in GitHub Desktop.
Obsidian Script to Show Previous/Next Day Link

Obsidian Script to Show Previous/Next Day Link

Caution!
It is always a good practice to check the code-snippet and see what you're running. Although I personally use this, if you use it, use at your own risk.

Requirements

You need to have Dataview plugin installed and also "Enable JavaScript Queries" enabled.

Setup

  1. Download the dailyNav.js file here and put it somewhere in your vault (i.e. <YOUR_VALULT>/scripts/dailyNav.js).
  2. If you are using date-format different from YYYY-MM-DD for your Daily notes, you need to adjust date-formate in relativeLink fuction in the js file to output accordingly.

Usage

Where you need to have the Previous/Next Day links (i.e. somewhere in Daily note template), use the snippet below:

```dataviewjs
dv.view('scripts/dailyNav');
```
function relativeLink(dv, date, offset, label) {
let offsetDate = date.plus({days: offset});
let link = dv.fileLink(offsetDate.toFormat('yyyy-MM-dd'), false, label);
if (dv.page(link) == null) {
return label;
} else {
return link;
}
}
function nextPreviousDayLinks(dv, separator, prevLabel, nextLabel) {
const day = dv.current().file.day ?? dv.api.luxon.DateTime.now();
dv.span(relativeLink(dv, day, -1, prevLabel) + separator + relativeLink(dv, day, 1, nextLabel));
}
input = input || {}
let separator = input.separator ?? ' - '
let prevLabel = input.prevLabel ?? "❮ Previous Day"
let nextLabel = input.nextLabel ?? "Next Day ❯"
nextPreviousDayLinks(dv, separator, prevLabel, nextLabel);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment