this post was submitted on 26 Jun 2023
4 points (83.3% liked)

ObsidianMD

3764 readers
1 users here now

Unofficial Lemmy community for https://obsidian.md

founded 1 year ago
MODERATORS
 

I would like to make a template but I would like to see some examples of what is possible to achieve.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 1 year ago

This is the code for showing the previous and next days, but only if they are created. So if you have June 26 and are on that note, but you don't have the 25th or 27th created but you do have the 24th created, it will display something like this ← 2023-06-24 | 2023-W26 | (none) → with the middle one here being the week.

I took this dataviewjs code I found from the obsidian forums and modified it slightly:

class PreviousNextDay {
    base_path = "a journal/"

    display(dv) {
        var none = '(none)';
        var t = dv.current().file.day ? dv.current().file.day.toISODate() : luxon.DateTime.now().toISODate();

        var year = moment(t, format).format("YYYY");
        
        var p = dv.pages('"' + this.base_path + year + '"').where(p => p.file.day).map(p => [p.file.name, p.file.day.toISODate()]).sort(p => p[1]);

        // Obsidian uses moment.js; Luxon’s format strings differ!
        var format = app['internalPlugins']['plugins']['daily-notes']['instance']['options']['format'] || 'YYYY-MM-DD';

        var week_format = 'gggg-[W]WW';
        var week = moment(t, format).format(week_format);

        var nav = [];
        var today = p.find(p => p[1] == t);
        var next = p.find(p => p[1] > t);
        var prev = undefined;
        p.forEach(function (p, i) {
            if (p[1] < t) {
                prev = p;
            }
        });
        nav.push(prev ? '[[' + prev[0] + ']]' : none);
        nav.push(today ? '[[' + week + ']]' : "N/A");
        nav.push(next ? '[[' + next[0] + ']]' : none);

        dv.paragraph(' ← ' + nav[0] + ' | ' + nav[1] + ' | ' + nav[2] + ' → ');
    }
}