Skip to content

Commit 7310566

Browse files
committed
fix: add weekly and monthly {{template}} replacements back
1 parent bc2e70e commit 7310566

File tree

3 files changed

+148
-22
lines changed

3 files changed

+148
-22
lines changed

src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ export default class PeriodicNotesPlugin extends Plugin {
213213
const templateContents = await getTemplateContents(this.app, config.templatePath);
214214
const renderedContents = applyTemplateTransformations(
215215
filename,
216+
granularity,
216217
date,
217218
format,
218219
templateContents

src/switcher/switcher.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,16 @@ export class NLDNavigator extends SuggestModal<DateNavigationItem> {
105105

106106
getSuggestions(query: string): DateNavigationItem[] {
107107
const dateInQuery = this.nlDatesPlugin.parseDate(query);
108+
const quickSuggestions = this.getDateSuggestions(query);
109+
110+
if (quickSuggestions.length) {
111+
return quickSuggestions;
112+
}
113+
108114
if (dateInQuery.moment.isValid()) {
109115
return this.getPeriodicNotesFromQuery(query, dateInQuery.moment);
110116
}
111-
112-
return this.getDateSuggestions(query);
117+
return [];
113118
}
114119

115120
getDateSuggestions(query: string): DateNavigationItem[] {
@@ -127,17 +132,17 @@ export class NLDNavigator extends SuggestModal<DateNavigationItem> {
127132
if (relativeExpr) {
128133
const reference = relativeExpr[1];
129134
return [
130-
getSuggestion(`${reference} week`, "week"),
131-
getSuggestion(`${reference} month`, "month"),
132-
// getSuggestion(`${reference} quarter`, "quarter"),
133-
getSuggestion(`${reference} year`, "year"),
134135
getSuggestion(`${reference} Sunday`, "day"),
135136
getSuggestion(`${reference} Monday`, "day"),
136137
getSuggestion(`${reference} Tuesday`, "day"),
137138
getSuggestion(`${reference} Wednesday`, "day"),
138139
getSuggestion(`${reference} Thursday`, "day"),
139140
getSuggestion(`${reference} Friday`, "day"),
140141
getSuggestion(`${reference} Saturday`, "day"),
142+
getSuggestion(`${reference} week`, "week"),
143+
getSuggestion(`${reference} month`, "month"),
144+
// getSuggestion(`${reference} quarter`, "quarter"), TODO include once nldates supports quarters
145+
getSuggestion(`${reference} year`, "year"),
141146
]
142147
.filter((items) => activeGranularities.includes(items.granularity))
143148
.filter((items) => items.label.toLowerCase().startsWith(query));

src/utils.ts

+136-16
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,156 @@ export function isMetaPressed(e: MouseEvent | KeyboardEvent): boolean {
1111
return Platform.isMacOS ? e.metaKey : e.ctrlKey;
1212
}
1313

14+
function getDaysOfWeek(): string[] {
15+
const { moment } = window;
16+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
17+
let weekStart = (moment.localeData() as any)._week.dow;
18+
const daysOfWeek = [
19+
"sunday",
20+
"monday",
21+
"tuesday",
22+
"wednesday",
23+
"thursday",
24+
"friday",
25+
"saturday",
26+
];
27+
28+
while (weekStart) {
29+
daysOfWeek.push(daysOfWeek.shift()!);
30+
weekStart--;
31+
}
32+
return daysOfWeek;
33+
}
34+
35+
export function getDayOfWeekNumericalValue(dayOfWeekName: string): number {
36+
return getDaysOfWeek().indexOf(dayOfWeekName.toLowerCase());
37+
}
38+
1439
export function applyTemplateTransformations(
1540
filename: string,
41+
granularity: Granularity,
1642
date: Moment,
1743
format: string,
1844
rawTemplateContents: string
1945
): string {
20-
return rawTemplateContents
46+
let templateContents = rawTemplateContents;
47+
48+
templateContents = rawTemplateContents
2149
.replace(/{{\s*date\s*}}/gi, filename)
2250
.replace(/{{\s*time\s*}}/gi, window.moment().format("HH:mm"))
23-
.replace(/{{\s*title\s*}}/gi, filename)
24-
.replace(
25-
/{{\s*(date|time)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi,
51+
.replace(/{{\s*title\s*}}/gi, filename);
52+
53+
if (granularity === "day") {
54+
templateContents = templateContents
55+
.replace(/{{\s*yesterday\s*}}/gi, date.clone().subtract(1, "day").format(format))
56+
.replace(/{{\s*tomorrow\s*}}/gi, date.clone().add(1, "d").format(format))
57+
.replace(
58+
/{{\s*(date|time)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi,
59+
(_, _timeOrDate, calc, timeDelta, unit, momentFormat) => {
60+
const now = window.moment();
61+
const currentDate = date.clone().set({
62+
hour: now.get("hour"),
63+
minute: now.get("minute"),
64+
second: now.get("second"),
65+
});
66+
if (calc) {
67+
currentDate.add(parseInt(timeDelta, 10), unit);
68+
}
69+
70+
if (momentFormat) {
71+
return currentDate.format(momentFormat.substring(1).trim());
72+
}
73+
return currentDate.format(format);
74+
}
75+
);
76+
}
77+
78+
if (granularity === "week") {
79+
templateContents = templateContents.replace(
80+
/{{\s*(sunday|monday|tuesday|wednesday|thursday|friday|saturday)\s*:(.*?)}}/gi,
81+
(_, dayOfWeek, momentFormat) => {
82+
const day = getDayOfWeekNumericalValue(dayOfWeek);
83+
return date.weekday(day).format(momentFormat.trim());
84+
}
85+
);
86+
}
87+
88+
if (granularity === "month") {
89+
templateContents = templateContents.replace(
90+
/{{\s*(month)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi,
2691
(_, _timeOrDate, calc, timeDelta, unit, momentFormat) => {
2792
const now = window.moment();
28-
const currentDate = date.clone().set({
29-
hour: now.get("hour"),
30-
minute: now.get("minute"),
31-
second: now.get("second"),
32-
});
93+
const monthStart = date
94+
.clone()
95+
.startOf("month")
96+
.set({
97+
hour: now.get("hour"),
98+
minute: now.get("minute"),
99+
second: now.get("second"),
100+
});
33101
if (calc) {
34-
currentDate.add(parseInt(timeDelta, 10), unit);
102+
monthStart.add(parseInt(timeDelta, 10), unit);
35103
}
36104

37105
if (momentFormat) {
38-
return currentDate.format(momentFormat.substring(1).trim());
106+
return monthStart.format(momentFormat.substring(1).trim());
39107
}
40-
return currentDate.format(format);
108+
return monthStart.format(format);
41109
}
42-
)
43-
.replace(/{{\s*yesterday\s*}}/gi, date.clone().subtract(1, "day").format(format))
44-
.replace(/{{\s*tomorrow\s*}}/gi, date.clone().add(1, "d").format(format));
110+
);
111+
}
112+
113+
if (granularity === "quarter") {
114+
templateContents = templateContents.replace(
115+
/{{\s*(quarter)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi,
116+
(_, _timeOrDate, calc, timeDelta, unit, momentFormat) => {
117+
const now = window.moment();
118+
const monthStart = date
119+
.clone()
120+
.startOf("quarter")
121+
.set({
122+
hour: now.get("hour"),
123+
minute: now.get("minute"),
124+
second: now.get("second"),
125+
});
126+
if (calc) {
127+
monthStart.add(parseInt(timeDelta, 10), unit);
128+
}
129+
130+
if (momentFormat) {
131+
return monthStart.format(momentFormat.substring(1).trim());
132+
}
133+
return monthStart.format(format);
134+
}
135+
);
136+
}
137+
138+
if (granularity === "year") {
139+
templateContents = templateContents.replace(
140+
/{{\s*(year)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi,
141+
(_, _timeOrDate, calc, timeDelta, unit, momentFormat) => {
142+
const now = window.moment();
143+
const monthStart = date
144+
.clone()
145+
.startOf("year")
146+
.set({
147+
hour: now.get("hour"),
148+
minute: now.get("minute"),
149+
second: now.get("second"),
150+
});
151+
if (calc) {
152+
monthStart.add(parseInt(timeDelta, 10), unit);
153+
}
154+
155+
if (momentFormat) {
156+
return monthStart.format(momentFormat.substring(1).trim());
157+
}
158+
return monthStart.format(format);
159+
}
160+
);
161+
}
162+
163+
return templateContents;
45164
}
46165

47166
export function getFormat(calendarSet: CalendarSet, granularity: Granularity): string {
@@ -72,6 +191,7 @@ export async function applyPeriodicTemplateToFile(
72191
);
73192
const renderedContents = applyTemplateTransformations(
74193
file.basename,
194+
metadata.granularity,
75195
metadata.date,
76196
format,
77197
templateContents
@@ -157,7 +277,6 @@ async function ensureFolderExists(app: App, path: string): Promise<void> {
157277
}
158278

159279
export function getRelativeDate(granularity: Granularity, date: Moment) {
160-
const today = window.moment().startOf("day");
161280
if (granularity == "week") {
162281
const thisWeek = window.moment().startOf(granularity);
163282
const fromNow = window.moment(date).diff(thisWeek, "week");
@@ -170,6 +289,7 @@ export function getRelativeDate(granularity: Granularity, date: Moment) {
170289
}
171290
return window.moment.duration(fromNow, granularity).humanize(true);
172291
} else if (granularity === "day") {
292+
const today = window.moment().startOf("day");
173293
const fromNow = window.moment(date).from(today);
174294
return window.moment(date).calendar(null, {
175295
lastWeek: "[Last] dddd",

0 commit comments

Comments
 (0)