silverbulletmd / silverbullet

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

onApplicationStart Hook for Space Scripts #816

Closed simone-viozzi closed 3 months ago

simone-viozzi commented 3 months ago

Currently, SilverBullet lacks a direct mechanism to execute scripts or hooks purely based on the application start event.

This limitation prevents automating certain tasks that users might want to run immediately as the application loads, such as preparing the workspace for the day ahead or creating pages from templates if they don't already exist.

Proposed solution

I propose the introduction of an onApplicationStart hook within the Space Script API. This hook would allow users to register functions that should be executed automatically when SilverBullet starts.

Here's a conceptual example of how this might look within a Space Script:

silverbullet.registerOnApplicationStart(async () => {
  // Your code to execute on application start
  console.log("SilverBullet has started!");
  // Example: Automatically creating a page from a template if it doesn't exist
  const today = new Date().toISOString().split('T')[0]; // YYYY-MM-DD format
  const pageName = `notes/Agenda/${today}`;
  const pageExists = await syscall("space.pageExists", pageName);
  if (!pageExists) {
    await syscall("space.createPageFromTemplate", {
      templatePageName: "Daily Agenda Template",
      targetPageName: pageName
    });
  }
});

Example Use Case

As an example use case for this feature, consider the scenario where a user wants to automatically create a "Daily Agenda" page from a template each day when they first start SilverBullet.

If the page for the current day already exists, the script should do nothing.

zefhemel commented 3 months ago

You can already do this with https://silverbullet.md/Space%20Script#Custom%20event%20listeners

You can listen to the editor:init event if you're interested in doing something in the client whenever it has loaded, and I think system:ready will trigger just on the server when it has just booted. In your case you probably want to use editor:init.

simone-viozzi commented 3 months ago

thank you i did not know that.

is there a list of all events?

zefhemel commented 3 months ago

Not really, that's why the linked page has an example space script to discover them.