Connect (irregular) daily notes?

What I’m trying to do

I have the Daily Notes plugin, and it connects with the previous and next day. Which would be fine if I write a daily note everyday. But I write a daily note every few days. Is it possible to link the daily notes to the next and previous existing note?

Things I have tried

I searched on google and reddit, but alas - no solution. I feel that it must be a common question. I can probably solve it with a python script, but want to avoid reinventing the wheel…

1 Like

Suggested approach

  • Templatr script which includes dataviewjs query to populate [[xxx.md|Previous note]] in daily note
  • Dataviewjs query builds an array of all daily notes (via folder or tags) and calculates number of days between them and current date (using moment and file name or creation date)
  • The daily note file with the lowest number of days between it and today is the file linked as xxx.md

There are many approaches to this kind of connecting notes, and in the thread below @npatch and I discuss and highlight two approaches. My approach which is a dynamic approach calculating the links when we display a daily note, and npatch’es solution having static links which gets updated whenever a new daily note is created. Both rely upon the Templater and Dataview plugins.

Code examples for both approaches are given in the thread above. Hope this helps!

1 Like

I may add that this would also be useful to have on the web on Obsidian Publish sites.

I publish my daily notes, and I’m missing the standard navigation element typically present on such pages: Move to Next Note & Move to Previous Note. The two links would ideally be placed at the bottom of the page.

(Nope, having them only in the left sidebar isn’t the same thing, especially not on the phone where the sidebar isn’t constantly visible…)

Given that last requirement of yours to have it available in published versions, and the fact that queries (and plugins in general ?) don’t get published, you’d need to go for the static version of @npatch.

I wrote a Python script to connect all existing notes. It seems the Obsidian solution is more difficult than every few weeks run the python script.

Btw, I also had some notes without tags - so added this as well.

import os

def update_obsidian_links_v3(directory):
    
    # Sort the markdown files
    files = sorted([f for f in os.listdir(directory) if f.endswith('.md')])

    print(f"Found {len(files)} markdown files.")

    for i, file in enumerate(files):
        current_file = file
        prev_file = files[i - 1] if i > 0 else None
        next_file = files[i + 1] if i < len(files) - 1 else None

        print(f"Processing file: {current_file}, Prev: {prev_file}, Next: {next_file}")

        with open(os.path.join(directory, current_file), 'r+') as f:
            lines = f.readlines()
            
            # Construct the new link
            new_link = f"<< [[{prev_file.replace('.md', '') if prev_file else 'N/A'}]] | [[{next_file.replace('.md', '') if next_file else 'N/A'}]] >>"
            link_found = False
            print(new_link)
            # Replace or insert the link
            for j, line in enumerate(lines):
                if line.strip().startswith('<<'):
                    lines[j] = new_link + '\n'
                    link_found = True
                    break

            # If no link was found, insert the new link after the header
            if not link_found:
                print("no link found")
                for j, line in enumerate(lines):
                    if line.startswith('tags::'):
                        lines.insert(j + 1, new_link + '\n')
                        break

            f.seek(0)
            f.write(''.join(lines))
            f.truncate()

    return "Links updated successfully."

def add_daily_notes_tag(directory):
    files = sorted([f for f in os.listdir(directory) if f.endswith('.md')])

    for file in files:
        with open(os.path.join(directory, file), 'r+') as f:
            lines = f.readlines()
            if not any(line.strip() == 'tags:: [[+Daily Notes]]' for line in lines):
                lines.insert(0, 'tags:: [[+Daily Notes]]\n')
                f.seek(0)
                f.write(''.join(lines))
                f.truncate()

    return "Tags updated successfully."


directory_path = 'your path'

add_daily_notes_tag(directory_path)
update_obsidian_links_v3(directory_path)


It’s good that you found a solution you like. The only concern I’ve got regarding your script is that you use a readonly file handle, f, to rewrite the file whilst you’re still reading the file… That’s kind of dangerous in my book.

Another approach would be to do weekly or monthly notes instead of daily ones.

2 Likes

FWIW, @mamo , my concept is to use templater to generate a new daily and at that point, I lookup the files previous and next to this daily and update all links involved, much like how you’d handle doubly linked lists in any programming language really. I just use the templater’s ability to run js queries for Previous and Next link in the template to do the updates to other files as well. I’ve been using it since I made it and I don’t have to bother with it anymore. It only uses templater basically for this specific part of the problem. You could reach the same result faster with dataviewjs as @holroy suggests. I could have done his way with way less trouble but it was a kind of pet peeve of mine I guess. To each his own. I still thank him for some of his insights and patience xD.

I do agree with @CawlinTeffid’s suggestion though. If you can avoid this altogether, it’s worth it. I use dailys to log e.g. why a certain approach at implementing sth at work did or didn’t work, because ask me in 5 months, I will “sort of” remember. So this is a way to avoid looking like I have no idea what I’ve done by saying “trust me”. A weekly could have worked perhaps if some things were actual tasks/targets for the project, but it ain’t that well organized, so I opted for a more verbose logging for my sanity.

Good luck regardless.

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