silverbulletmd / silverbullet

The hackable notebook
https://silverbullet.md
MIT License
2.04k stars 141 forks source link

Home button can not trigger space script function in file name #801

Open SONDLecT opened 3 months ago

SONDLecT commented 3 months ago

I use a space script function to add some {{this[day]}} / {{last[day]}} / {{next[day]}} functions to insert dates into my templates/pages.


function computeDay(dayNumber, offset = 0) {
  let today = Temporal.Now.plainDateISO();
  const currentDayOfWeek = today.dayOfWeek;
  offset = (dayNumber == daysMapping["Sunday"]) ? (offset - 7) : offset;
  const daysUntil = ((dayNumber - currentDayOfWeek + 7) % 7) + offset;
  const target = today.add({days: daysUntil});

  return target.toString();
}

// mapping days to their respective day numbers 
const daysMapping = {
  "Sunday": 0, "Monday": 1, "Tuesday": 2,
  "Wednesday": 3, "Thursday": 4, "Friday": 5,
  "Saturday": 6,
};

for (const [day, dayNumber] of Object.entries(daysMapping)) {
  // Register a function for getting "last" day.
  silverbullet.registerFunction({name: `last${day}`}, () => computeDay(dayNumber, -7));

  // Register a function for getting "this" day.
  silverbullet.registerFunction({name: `this${day}`}, () => computeDay(dayNumber));

  // Register a function for getting "next" day.
  silverbullet.registerFunction({name: `next${day}`}, () => computeDay(dayNumber, 7));
}

I’ve set my SETTINGS index page to /Journal/Week/{{thisSunday}}, hoping to create a weekly note that resets every Sunday. However; if I click the home button the note that generates is /Journal/Week/undefined; but if I just load the url of my SilverBullet instance the index loads correctly as /Journal/Week/2024-03-02 or whatever is expected. I’ve also noticed that if I use sync mode even that approach renders “undefined” instead of the proper YYYY-MM-DD.