I’m not really familiar with java script, so this might be a dumb question. But I find it not working to try to chaining multiple function calls in the template.
My case is trying to do the following
---
description: "Open your weekly note page"
tags: template
hooks.newPage:
suggestedName: "Journal/Week/{{currentyear}}-W{{weekNumberFor(today())}}"
forPrefix: "Journal/Week/"
confirmName: false
openIfExists: true
command: "Open Weekly Note"
key: "Alt-Shift-w"
frontmatter:
tags: weekly
week: "{{basename(@page.name}}"
---
Time Period: {{weekBeginFor(basename(@page.name))}} # this is the line that is not working
## Motivation
|^|
## Goals
## Changelog
I can get the proper value when calling {{basename(@page.name)}}
but when I use this as the variable to the function weekBeginFor
, in side the function it always receive empty string. I can provide the relevant functions definition in below. But I think it is really weird behavior for some JS beginner like myself.
Below are related space-script functions:
silverbullet.registerFunction({ name: 'weekBeginFor' }, (weekString) => {
// Use regex to extract year and week number
const regex = /(\d{4})-W(\d{1,2})/;
const match = weekString.match(regex);
if (!match) {
throw new Error(`Invalid week string format. Expected format: 'YYYY-Www', got ${String(weekString)}`);
}
const year = Number(match[1]);
const week = Number(match[2]);
// Calculate the first day of the specified week
const weekStartDate = Temporal.PlainDate.from({ year, month: 1, day: 1 });
// Determine the first day of the first week of the year (Monday)
const firstMondayOfYear = weekStartDate.with({
day: 1,
}).subtract({ days: (weekStartDate.dayOfWeek - 1) });
// Now move forward by (week - 1) weeks to get to the desired week
const weekStart = firstMondayOfYear.add({ weeks: week - 1 });
// Return the first day of the week (Monday)
return weekStart.toString();
});
function getBasename(path) {
// Remove any trailing slashes
path = path.replace(/\/+$/, '');
// Split the path by slashes and get the last part
const parts = path.split('/');
return parts.pop();
}
silverbullet.registerFunction(
{name: "basename"},
(pth) => {
return getBasename(pth)
}
)