silverbulletmd / silverbullet

The knowledge tinkerer's notebook
https://silverbullet.md
MIT License
2.35k stars 169 forks source link

Custom SpaceScript Functions not Recognized in indexPage Setting #785

Closed SONDLecT closed 7 months ago

SONDLecT commented 7 months ago

Issue Description

When configuring the indexPage setting in SilverBullet to use a custom SpaceScript function (e.g., {{thisSunday}}), the rendered index page title displays "undefined" instead of the expected date value returned by the function. This behavior suggests that custom SpaceScript functions are not being recognized or evaluated correctly within the indexPage setting context. However, these custom functions work as expected when used in other template contexts, such as page templates.

Steps to Reproduce

  1. Define custom SpaceScript functions in SilverBullet that calculate dates, such as thisSunday, thisMonday, etc.
  2. Configure the indexPage setting in the SETTINGS page to use one of these custom functions, e.g., indexPage: "Journal/Week/{{thisSunday}}".
  3. Reload SilverBullet and observe the index page's title.

Expected Behavior

The index page title should render with the calculated date from the custom SpaceScript function, e.g., "Journal/Week/2024-03-10".

Actual Behavior

The index page title displays as "Journal/Week/undefined", indicating that the custom SpaceScript function's return value is not being recognized or inserted correctly.

Relevant Code

Here is an example of a custom SpaceScript function defined for thisSunday:

silverbullet.registerFunction({name: "thisSunday"}, () => {
  const today = new Date();
  const currentDayOfWeek = today.getDay();
  const daysUntilThisSunday = (7 - currentDayOfWeek) % 7;
  const thisSundayDate = new Date(today.getTime() + (daysUntilThisSunday * 24 * 60 * 60 * 1000));

  return `${thisSundayDate.getFullYear()}-${(thisSundayDate.getMonth() + 1).toString().padStart(2, '0')}-${thisSundayDate.getDate().toString().padStart(2, '0')}`;
})

silverbullet.registerFunction({name: "thisMonday"}, () => {
  const today = new Date();
  const currentDayOfWeek = today.getDay();
  const daysUntilThisMonday = (1 - currentDayOfWeek + 7) % 7;
  const thisMondayDate = new Date(today.getTime() + (daysUntilThisMonday * 24 * 60 * 60 * 1000));

  return `${thisMondayDate.getFullYear()}-${(thisMondayDate.getMonth() + 1).toString().padStart(2, '0')}-${thisMondayDate.getDate().toString().padStart(2, '0')}`;
})

silverbullet.registerFunction({name: "thisTuesday"}, () => {
  const today = new Date();
  const currentDayOfWeek = today.getDay();
  const daysUntilThisTuesday = (2 - currentDayOfWeek + 7) % 7;
  const thisTuesdayDate = new Date(today.getTime() + (daysUntilThisTuesday * 24 * 60 * 60 * 1000));

  return `${thisTuesdayDate.getFullYear()}-${(thisTuesdayDate.getMonth() + 1).toString().padStart(2, '0')}-${thisTuesdayDate.getDate().toString().padStart(2, '0')}`;
})

silverbullet.registerFunction({name: "thisWednesday"}, () => {
  const today = new Date();
  const currentDayOfWeek = today.getDay();
  const daysUntilThisWednesday = (3 - currentDayOfWeek + 7) % 7;
  const thisWednesdayDate = new Date(today.getTime() + (daysUntilThisWednesday * 24 * 60 * 60 * 1000));

  return `${thisWednesdayDate.getFullYear()}-${(thisWednesdayDate.getMonth() + 1).toString().padStart(2, '0')}-${thisWednesdayDate.getDate().toString().padStart(2, '0')}`;
})

silverbullet.registerFunction({name: "thisThursday"}, () => {
  const today = new Date();
  const currentDayOfWeek = today.getDay();
  const daysUntilThisThursday = (4 - currentDayOfWeek + 7) % 7;
  const thisThursdayDate = new Date(today.getTime() + (daysUntilThisThursday * 24 * 60 * 60 * 1000));

  return `${thisThursdayDate.getFullYear()}-${(thisThursdayDate.getMonth() + 1).toString().padStart(2, '0')}-${thisThursdayDate.getDate().toString().padStart(2, '0')}`;
})

silverbullet.registerFunction({name: "thisFriday"}, () => {
  const today = new Date();
  const currentDayOfWeek = today.getDay();
  const daysUntilThisFriday = (5 - currentDayOfWeek + 7) % 7;
  const thisFridayDate = new Date(today.getTime() + (daysUntilThisFriday * 24 * 60 * 60 * 1000));

  return `${thisFridayDate.getFullYear()}-${(thisFridayDate.getMonth() + 1).toString().padStart(2, '0')}-${thisFridayDate.getDate().toString().padStart(2, '0')}`;
})

silverbullet.registerFunction({name: "thisSaturday"}, () => {
  const today = new Date();
  const currentDayOfWeek = today.getDay();
  const daysUntilThisSaturday = (6 - currentDayOfWeek + 7) % 7;
  const thisSaturdayDate = new Date(today.getTime() + (daysUntilThisSaturday * 24 * 60 * 60 * 1000));

  return `${thisSaturdayDate.getFullYear()}-${(thisSaturdayDate.getMonth() + 1).toString().padStart(2, '0')}-${thisSaturdayDate.getDate().toString().padStart(2, '0')}`;
})

Additional Information

This issue was observed while trying to set up a dynamic weekly journal entry system in SilverBullet. Custom SpaceScript functions are successfully recognized and utilized in other template scenarios within SilverBullet.