Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(calendar): name the title template's year count ${years}
Elian Doran65d167fab940
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 1ac0f9f207..1250107d19 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -782,7 +782,7 @@"calendar_slot_duration": "How long each timeslot of a calendar lasts, as <code>HH:MM:SS</code>. Defaults to 15 minutes.","calendar_slot_label_interval": "How often the timeslots of a calendar are labeled, as <code>HH:MM:SS</code>. Defaults to an hour.","calendar_title": "Names the attribute an event takes its title from, instead of the note title. Either a label or a relation, written without <code>#</code> or <code>~</code>.",- "calendar_title_template": "How an event is titled on the calendar, filled in for every occurrence of a repeating one. Reads <code>${title}</code>, <code>${age}</code> (whole years since the start date) and the dates <code>${date}</code> and <code>${startDate}</code>, e.g. <code>${title} (${age})</code> on a yearly birthday.",+ "calendar_title_template": "How an event is titled on the calendar, filled in for every occurrence of a repeating one. Reads <code>${title}</code>, <code>${years}</code> (whole years since the start date) and the dates <code>${date}</code> and <code>${startDate}</code>, e.g. <code>${title} (${years})</code> on a yearly birthday.","calendar_displayed_attributes": "Comma-delimited attribute names whose values are shown on the event. A relation is shown as the title of the note it points at.","calendar_start_date": "Names the label an event reads its start date from, instead of <code>#startDate</code>. Written without the <code>#</code>.","calendar_end_date": "Names the label an event reads its end date from, instead of <code>#endDate</code>.",diff --git a/apps/client/src/widgets/collections/calendar/event_builder.spec.ts b/apps/client/src/widgets/collections/calendar/event_builder.spec.tsindex 7008b8bebf..dc1d558b83 100644--- a/apps/client/src/widgets/collections/calendar/event_builder.spec.ts+++ b/apps/client/src/widgets/collections/calendar/event_builder.spec.ts@@ -470,14 +470,14 @@ describe("Recurrence", () => {"#startDate": "1990-05-12","#startTime": "09:00","#recurrence": "RRULE:FREQ=YEARLY",- "#calendar:titleTemplate": "${title} (${age})"+ "#calendar:titleTemplate": "${title} (${years})"}]);const events = await buildEvents(noteIds);expect(events[0]).toMatchObject({title: "John Smith",- titleTemplate: "${title} (${age})",+ titleTemplate: "${title} (${years})",// The date as the label holds it, the start time not yet worked into it.seriesStart: "1990-05-12"});@@ -610,25 +610,25 @@ describe("buildOccurrenceTitle", () => {});it("counts the whole years from the start date to the occurrence", () => {- expect(buildOccurrenceTitle(occurrence("${title} (${age})", new Date(2026, 4, 12))))+ expect(buildOccurrenceTitle(occurrence("${title} (${years})", new Date(2026, 4, 12)))).toBe("John Smith (36)");- expect(buildOccurrenceTitle(occurrence("${title} turns ${age}", new Date(2027, 4, 12))))+ expect(buildOccurrenceTitle(occurrence("${title} turns ${years}", new Date(2027, 4, 12)))).toBe("John Smith turns 37");// The day the series itself starts on, which is where the count starts.- expect(buildOccurrenceTitle(occurrence("${title} (${age})", new Date(1990, 4, 12))))+ expect(buildOccurrenceTitle(occurrence("${title} (${years})", new Date(1990, 4, 12)))).toBe("John Smith (0)");});it("reads both the occurrence's date and the series'", () => {const template =- "${title}, born ${startDate.format('YYYY')}, turns ${age} in ${date.format('YYYY')}";+ "${title}, born ${startDate.format('YYYY')}, turns ${years} in ${date.format('YYYY')}";expect(buildOccurrenceTitle(occurrence(template, new Date(2026, 4, 12)))).toBe("John Smith, born 1990, turns 36 in 2026");});it("falls back to the plain title for a template the evaluator rejects, saying so once", () => {const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});- const broken = occurrence("${nmae} (${age})", new Date(2026, 4, 12));+ const broken = occurrence("${nmae} (${years})", new Date(2026, 4, 12));expect(buildOccurrenceTitle(broken)).toBe("John Smith");expect(buildOccurrenceTitle(broken)).toBe("John Smith");diff --git a/apps/client/src/widgets/collections/calendar/event_builder.ts b/apps/client/src/widgets/collections/calendar/event_builder.tsindex a41d04fce3..e731c1a70f 100644--- a/apps/client/src/widgets/collections/calendar/event_builder.ts+++ b/apps/client/src/widgets/collections/calendar/event_builder.ts@@ -226,7 +226,7 @@ export async function buildEvent(note: FNote, {** Runs as the chip is drawn rather than in {@link buildEvent} because FullCalendar expands a* `#recurrence` rule into occurrences itself: a repeating event is one {@link EventInput} with- * one title, so `${age}` — the whole years from `#startDate` to this occurrence — is only known+ * one title, so `${years}` — the whole years from `#startDate` to this occurrence — is only known* here.** A template {@link evaluateTemplate} rejects leaves the event with its plain title.@@ -238,7 +238,7 @@ export function buildOccurrenceTitle({ title, start, extendedProps }: Occurrence}// Both dates as plain days, without the time: a DST shift between them would otherwise take an- // hour off the span and round `age` down by a year.+ // hour off the span and round `years` down by a year.const occurrenceDay = formatDateToLocalISO(start);const seriesStartDay = dayjs(seriesStart.split("T")[0]);const occurrence = occurrenceDay ? dayjs(occurrenceDay) : seriesStartDay;@@ -246,7 +246,7 @@ export function buildOccurrenceTitle({ title, start, extendedProps }: Occurrencetry {return evaluateTemplate(titleTemplate, {title,- age: occurrence.diff(seriesStartDay, "year"),+ years: occurrence.diff(seriesStartDay, "year"),date: occurrence,startDate: seriesStartDay});diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.htmlindex 725f92acf1..368dfad9e6 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html@@ -549,14 +549,13 @@ class="table"><h3>Displaying an age on a recurring event</h3><p>A birthday or an anniversary repeats every year, and what is interestingabout each occurrence is the number of years it marks. <code>#calendar:titleTemplate</code> sets- the title an event is displayed with, and is filled in again for every occurrence- the recurrence rule produces:</p><pre><code class="language-text-x-trilium-auto">#startDate=1990-05-12 #recurrence="FREQ=YEARLY" #calendar:titleTemplate="${title} (${age})"</code></pre>-<p>The event is then displayed as “John Smith (35)” in 2025, “John- Smith (36)” in 2026, and so on. Only the calendar is affected; the- note keeps its own title.</p>+ the title an event is displayed with, and is filled in again for every+ occurrence the recurrence rule produces:</p><pre><code class="language-text-x-trilium-auto">#startDate=1990-05-12 #recurrence="FREQ=YEARLY" #calendar:titleTemplate="${title} (${years})"</code></pre>+<p>The event is then displayed as “John Smith (35)” in 2025, “John Smith+ (36)” in 2026, and so on. Only the calendar is affected; the note keeps+ its own title.</p><p>The following values can be used in the template:</p>-<figure-class="table">+<figure class="table"><table><thead><tr>@@ -572,7 +571,7 @@ class="table">or the attribute <code>#calendar:title</code> points to.</td></tr><tr>- <td><code>${age}</code>+ <td><code>${years}</code></td><td>The whole years between <code>#startDate</code> and the occurrence beingdisplayed. It is <code>0</code> on the first occurrence.</td>@@ -592,9 +591,10 @@ class="table"></table></figure><p>To use the same template for every event of a calendar, make the label- inheritable on the Collection note:</p><pre><code class="language-text-x-trilium-auto">#calendar:titleTemplate(inheritable)="${title} (${age})"</code></pre>-<aside class="admonition note">- <p>A template that cannot be read — an unknown value, or an expression- other than a value and the <code>format</code> call shown above — leaves- the event with its plain title and reports the reason in the browser console.</p>-</aside>+ inheritable on the Collection note:</p><pre><code class="language-text-x-trilium-auto">#calendar:titleTemplate(inheritable)="${title} (${years})"</code></pre>+<aside+class="admonition note">+ <p>A template that cannot be read — an unknown value, or an expression other+ than a value and the <code>format</code> call shown above — leaves the event+ with its plain title and reports the reason in the browser console.</p>+</aside>\ No newline at end of filediff --git a/docs/User Guide/User Guide/Collections/Calendar.md b/docs/User Guide/User Guide/Collections/Calendar.mdindex e5d8e4c7ce..dec2a93b3c 100644--- a/docs/User Guide/User Guide/Collections/Calendar.md+++ b/docs/User Guide/User Guide/Collections/Calendar.md@@ -305,7 +305,7 @@ Moreover, if there are more relations of the same name, they will be displayed aA birthday or an anniversary repeats every year, and what is interesting about each occurrence is the number of years it marks. `#calendar:titleTemplate` sets the title an event is displayed with, and is filled in again for every occurrence the recurrence rule produces:```-#startDate=1990-05-12 #recurrence="FREQ=YEARLY" #calendar:titleTemplate="${title} (${age})"+#startDate=1990-05-12 #recurrence="FREQ=YEARLY" #calendar:titleTemplate="${title} (${years})"```The event is then displayed as “John Smith (35)” in 2025, “John Smith (36)” in 2026, and so on. Only the calendar is affected; the note keeps its own title.@@ -315,15 +315,15 @@ The following values can be used in the template:| Value | Description || --- | --- || `${title}` | The title the event would otherwise be displayed with: the note title, or the attribute `#calendar:title` points to. |-| `${age}` | The whole years between `#startDate` and the occurrence being displayed. It is `0` on the first occurrence. |+| `${years}` | The whole years between `#startDate` and the occurrence being displayed. It is `0` on the first occurrence. || `${date}` | The date of the occurrence being displayed, as a [day.js](https://day.js.org/docs/en/display/format) object. For example, `${date.format('YYYY')}`. || `${startDate}` | The date the event starts on, in the same form. For example, `${startDate.format('YYYY')}`. |To use the same template for every event of a calendar, make the label inheritable on the Collection note:```-#calendar:titleTemplate(inheritable)="${title} (${age})"+#calendar:titleTemplate(inheritable)="${title} (${years})"```> [!NOTE]-> A template that cannot be read — an unknown value, or an expression other than a value and the `format` call shown above — leaves the event with its plain title and reports the reason in the browser console.+> A template that cannot be read — an unknown value, or an expression other than a value and the `format` call shown above — leaves the event with its plain title and reports the reason in the browser console.\ No newline at end of filediff --git a/packages/commons/src/lib/safe_template.ts b/packages/commons/src/lib/safe_template.tsindex e6172ef524..f0e846fd4a 100644--- a/packages/commons/src/lib/safe_template.ts+++ b/packages/commons/src/lib/safe_template.ts@@ -13,7 +13,7 @@* - ${parentNote.getLabelValue('authorName')}* - ${note.title}* - ${note.dateCreatedObj.format('MM-DD')}- * - ${title} (${age})+ * - ${title} (${years})*//** Allowed method names that can be called on template variables. */