Skip to content

Instantly share code, notes, and snippets.

@kmalcaba
Last active July 22, 2023 15:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmalcaba/6e1bdb5689f32293d9091bc34bd54d34 to your computer and use it in GitHub Desktop.
Save kmalcaba/6e1bdb5689f32293d9091bc34bd54d34 to your computer and use it in GitHub Desktop.
Automatically insert birthday callouts for people's birthday in your daily notes in Obsidian

Birthday Callouts in Daily Notes

Description

Automatically insert birthday callouts for people's birthdays in your daily notes:

image

Edited 19/09/2022: Changed the alias part to use the updated structure (removed values)

Prerequisites

Make sure you have the following plugins installed and enabled:

  • Daily Notes
  • Templater
  • Admonition → for creating a custom callout
  • Dataview → enable JavaScript queries

Set up

  1. Create a custom callout in the Admonition plugin settings: image
  2. Create notes for each person's birthday with the file name as the person's full name
  3. In the YAML frontmatter, put the following:
    ---
    birthday: YYYY-MM-DD
    alias: [Nickname 1, Nickname 2, and so on]
    ---
    
  4. Feel free to add more info in your notes about the person, but only the above two is required. I use aliases so that nicknames will be shown instead of their full names. You can change it to display the file name instead by changing a variable value (step 6).
  5. Create a template note in the folder location Templater uses and insert the following code (scroll down to the next file in the gist). This is slightly modified from DataviewJS Snippet Showcase - #6 by Moonbase59 - Share & showcase - Obsidian Forum
  6. Set isAlias to false if you would like to use the file name instead of the alias.
  7. Change variable dir to the value of your CRM directory. Format: '"insert/directory/here"' (double quotes surrounded by single quotes).
  8. Insert the above template note in your Daily Notes template, like so:
    <%-tp.file.include("[[Birthday template]]")%>
    
    Again, don't forget the double quotes.
  9. Create a daily note. If a person in your CRM has a birthday, a callout should be generated, linking to that person's note.

<%* const title = tp.file.title; const start = moment(title.toString()); const dir = '"20 Personal/26 CRM"'; const dv = app.plugins.plugins.dataview.api; const isAlias = false;

function nextBirthday(birthday) { var bday = moment(birthday.toString()); var bdayNext = moment(bday).year(start.year()); if (bdayNext.isBefore(start, 'day')) { bdayNext.add(1, "year"); } return bdayNext; }

function showBirthday(birthday) { if (birthday) { const bday = moment(birthday.toString()); const bdayNext = nextBirthday(birthday); if (moment(bdayNext).isSame(start)) { return true; } else { return false; } } else { return false; } }

function sortByNextBirthday(a, b) { if (nextBirthday(a).isBefore(nextBirthday(b))) { return -1; } if (nextBirthday(a).isAfter(nextBirthday(b))) { return 1; } // they’re equal return 0; }

const people = dv.pages(dir).where(p => showBirthday(p.birthday)).sort(p => p.birthday, 'asc', sortByNextBirthday).values; console.log(people);

// if a birthday exists on the day if (people.length >= 1) { let content = \n> [!bday]\n> ; for (let i = 0; i < people.length; i++) { const person = people[i]; if (isAlias) { //use alias const alias = person.alias ? (Array.isArray(person.alias) ? person.alias[0] : person.alias) : person.file.name; content += [[${person.file.name}|${alias}]]; } else { //use file name const name = person.file.name; content += [[${person.file.name}]]; } //if multiple people have birthdays, insert a comma if (i != people.length - 1) { content += , ; } } tR += content; } %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment