Chaining function calls in Template

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)
  }
)

It seems to work for me. Could you give me the page name for which you are getting the issue. I tested the below and it works for me:

{{weekBeginFor(basename(@page.name))}}

Hi, @meain . I don’t use specific page name rather just init the weekly page via the command mentioned in the frontmatter.

The page name should be YYYY-WXX

Ahh, that is probably why your are getting empty. The regex pattern there(/(\d{4})-W(\d{1,2})/) looks for numbers and in your case it only find letters which is why it fails to process anything.

It is expecting something like 2024-W23, not the markers.