Access filename of new file in templates and space scripts during creation

Hi, I think I had exactly the same problem when trying to add previous/next links to my daily journal.

I tried to access this filename in a few ways and didn’t succeed. But I used a template to evaluate it only when the file is shown, and then if I don’t want it to be templated anymore (for example to get red links), I use the bake button in the corner.

The escapeDirective call is needed to evaluate it when shown, not when created.


This is at the top of my Library/Journal/New Page/Daily Note (the links are Polish for “yesterday tomorrow”)

```template
[[{{escapeDirective('pageAddDays(@page.name, -1)')}}|wczoraj]] [[{{escapeDirective('pageAddDays(@page.name, 1)')}}|jutro]]
```

screenshot with bake button

Definition of pageAddDays for completion
```space-script
silverbullet.registerFunction("pageAddDays:", (page, days) => {
  const parts = page.split("/");
  let date = parts[parts.length - 1];
  const plainDate = Temporal.PlainDate.from(date);
  const offsetDate = plainDate.add(Temporal.Duration.from({ days: Number(days) }));
  const result = parts.slice(0, -1);
  result.push(offsetDate.toString());
  return result.join("/");
});
```

Sorry I didn’t adapt it, I only have a moment to paste my current code. I hope that it still shows the approach to help with your solution :slight_smile:

2 Likes