@vermeulen.solutions/eventual 0.0.0-stage → 0.18.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +139 -2
- package/astro/EventList.astro +57 -0
- package/astro/admin.d.mts +3 -0
- package/astro/admin.mjs +158 -0
- package/astro/blueprint.d.mts +51 -0
- package/astro/blueprint.mjs +236 -0
- package/astro/cache.ts +82 -0
- package/astro/descriptor.mjs +7 -0
- package/astro/editor-compat.mjs +197 -0
- package/astro/editor-i18n.d.mts +8 -0
- package/astro/editor-i18n.mjs +157 -0
- package/astro/event-details.ts +29 -0
- package/astro/event-list.ts +56 -0
- package/astro/feed.ts +201 -0
- package/astro/install.d.mts +9 -0
- package/astro/install.mjs +66 -0
- package/astro/schema.ts +88 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +1585 -0
- package/dist/plugin.d.mts +6 -0
- package/dist/plugin.mjs +13 -0
- package/docs/collection-setup.md +46 -0
- package/docs/history.md +16 -0
- package/docs/native-modernization.md +99 -0
- package/docs/public-feed-caching.md +259 -0
- package/docs/registry-installation.md +13 -0
- package/docs/release-notes-0.14.0.md +27 -0
- package/docs/release-notes-0.14.1.md +40 -0
- package/docs/release-notes-0.15.0.md +18 -0
- package/docs/release-notes-0.16.0.md +7 -0
- package/docs/release-notes-0.17.0.md +7 -0
- package/docs/release-notes-0.18.0.md +8 -0
- package/docs/release-notes-0.18.1.md +7 -0
- package/docs/release-notes-0.18.2.md +6 -0
- package/docs/upgrade-0.13.0.md +21 -0
- package/docs/user-guide.md +40 -0
- package/emdash-plugin.jsonc +77 -0
- package/package.json +143 -4
- package/scripts/check-editor-installation.mjs +32 -0
- package/scripts/export-native-schema.mjs +28 -0
- package/scripts/upgrade-native-editor.mjs +296 -0
- package/src/domain/calendar-links.ts +80 -0
- package/src/domain/category.ts +32 -0
- package/src/domain/collections.ts +101 -0
- package/src/domain/date-time.ts +220 -0
- package/src/domain/description.ts +108 -0
- package/src/domain/event-data.ts +217 -0
- package/src/domain/event-expansion.ts +305 -0
- package/src/domain/event.ts +195 -0
- package/src/domain/icalendar.ts +165 -0
- package/src/domain/image.ts +18 -0
- package/src/domain/invocation-budget.ts +56 -0
- package/src/domain/messages.ts +8 -0
- package/src/domain/migration.ts +666 -0
- package/src/domain/native-calendar.ts +256 -0
- package/src/domain/native-references.ts +41 -0
- package/src/domain/native-source.ts +261 -0
- package/src/domain/native-validation.ts +366 -0
- package/src/domain/occurrences.ts +54 -0
- package/src/domain/portable-text.ts +180 -0
- package/src/domain/public-cache.ts +24 -0
- package/src/domain/recurrence-rule.ts +34 -0
- package/src/domain/recurrence.ts +350 -0
- package/src/domain/request-memo.ts +8 -0
- package/src/domain/schedule-history.ts +14 -0
- package/src/domain/text.ts +2 -0
- package/src/domain/time-entry.ts +23 -0
- package/src/domain/venue-adapter.ts +101 -0
- package/src/domain/venue.ts +42 -0
- package/src/public-event.ts +88 -0
- package/src/schema/blueprint.ts +315 -0
- package/src/storage.ts +257 -0
- package/transfer/index.d.mts +13 -0
- package/transfer/index.mjs +155 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { reject } from './messages';
|
|
2
|
+
import {
|
|
3
|
+
isDateOnly,
|
|
4
|
+
isValidTimeZone,
|
|
5
|
+
normalizeEventDates,
|
|
6
|
+
instantToLocalDateTime,
|
|
7
|
+
} from "./date-time";
|
|
8
|
+
import { validRecurrence } from "./recurrence-rule";
|
|
9
|
+
import { exceptionIdsMatchRecurrence, scheduledOccurrence } from "./recurrence";
|
|
10
|
+
import type { EventRecord } from "./event";
|
|
11
|
+
import { safeWebUrl } from "../../astro/event-details";
|
|
12
|
+
import { readCategories } from "./category";
|
|
13
|
+
|
|
14
|
+
function validInstant(value: unknown): value is string {
|
|
15
|
+
if (
|
|
16
|
+
typeof value !== "string" ||
|
|
17
|
+
!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?(?:Z|[+-]\d{2}:\d{2})$/.test(
|
|
18
|
+
value,
|
|
19
|
+
)
|
|
20
|
+
)
|
|
21
|
+
return false;
|
|
22
|
+
const clock = value.slice(11, 19).split(":");
|
|
23
|
+
return (
|
|
24
|
+
isDateOnly(value.slice(0, 10)) &&
|
|
25
|
+
Number.isFinite(Date.parse(value)) &&
|
|
26
|
+
Number(clock[0]) <= 23 &&
|
|
27
|
+
Number(clock[1]) <= 59 &&
|
|
28
|
+
(!clock[2] || Number(clock[2]) <= 59)
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function parseJson(value: unknown): unknown {
|
|
33
|
+
if (typeof value !== "string") return value ?? undefined;
|
|
34
|
+
if (!value.trim()) return undefined;
|
|
35
|
+
try {
|
|
36
|
+
return JSON.parse(value);
|
|
37
|
+
} catch {
|
|
38
|
+
reject("Invalid JSON.");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export const scheduleKeys = [
|
|
42
|
+
"start",
|
|
43
|
+
"end",
|
|
44
|
+
"start_date",
|
|
45
|
+
"end_date",
|
|
46
|
+
"all_day",
|
|
47
|
+
"timezone",
|
|
48
|
+
"recurrence",
|
|
49
|
+
"exceptions",
|
|
50
|
+
"occurrence_content",
|
|
51
|
+
"venue",
|
|
52
|
+
"venue_id",
|
|
53
|
+
"organizer_ref",
|
|
54
|
+
"organizer_id",
|
|
55
|
+
"location_type",
|
|
56
|
+
"event_status",
|
|
57
|
+
"calendar_uid",
|
|
58
|
+
"legacy_id",
|
|
59
|
+
"categories",
|
|
60
|
+
"virtual_url",
|
|
61
|
+
"external_url",
|
|
62
|
+
"image_url",
|
|
63
|
+
"organizer_details",
|
|
64
|
+
"previous_start_date",
|
|
65
|
+
"schedule_history",
|
|
66
|
+
];
|
|
67
|
+
export function normalizeNativeSchedule(
|
|
68
|
+
data: Record<string, unknown>,
|
|
69
|
+
options: { allowStaleOccurrenceCopy?: boolean } = {},
|
|
70
|
+
): Record<string, unknown> {
|
|
71
|
+
const result = { ...data };
|
|
72
|
+
const rawAllDay = data.all_day ?? data.allDay ?? false;
|
|
73
|
+
const allDay = rawAllDay === 0 ? false : rawAllDay === 1 ? true : rawAllDay;
|
|
74
|
+
if (typeof allDay !== "boolean")
|
|
75
|
+
reject("all_day must be a boolean.");
|
|
76
|
+
const timezone = data.timezone ?? "Europe/Zurich";
|
|
77
|
+
if (typeof timezone !== "string" || !isValidTimeZone(timezone))
|
|
78
|
+
reject("Invalid IANA timezone.");
|
|
79
|
+
const rawStart = allDay ? (data.start_date ?? data.start) : data.start;
|
|
80
|
+
const rawEnd = allDay ? (data.end_date ?? data.end) : data.end;
|
|
81
|
+
if (
|
|
82
|
+
typeof rawStart !== "string" ||
|
|
83
|
+
typeof rawEnd !== "string" ||
|
|
84
|
+
!rawStart ||
|
|
85
|
+
!rawEnd
|
|
86
|
+
)
|
|
87
|
+
reject("Start and end are required.");
|
|
88
|
+
let start = rawStart;
|
|
89
|
+
let end = rawEnd;
|
|
90
|
+
if (allDay) {
|
|
91
|
+
if (
|
|
92
|
+
(data.start_date != null && !isDateOnly(start)) ||
|
|
93
|
+
(data.end_date != null && !isDateOnly(end))
|
|
94
|
+
)
|
|
95
|
+
reject("All-day date fields must use YYYY-MM-DD.");
|
|
96
|
+
// Compatibility with ISO all-day records; new editing uses explicit civil-date fields.
|
|
97
|
+
if (
|
|
98
|
+
[start, end].some((value) => !isDateOnly(value) && !validInstant(value))
|
|
99
|
+
)
|
|
100
|
+
reject("Invalid all-day date representation.");
|
|
101
|
+
start = start.slice(0, 10);
|
|
102
|
+
end = end.slice(0, 10);
|
|
103
|
+
if (!isDateOnly(start) || !isDateOnly(end) || end < start)
|
|
104
|
+
reject("Invalid inclusive all-day dates.");
|
|
105
|
+
result.start_date = start;
|
|
106
|
+
result.end_date = end;
|
|
107
|
+
delete result.start;
|
|
108
|
+
delete result.end;
|
|
109
|
+
} else {
|
|
110
|
+
const instant = (s: string) => {
|
|
111
|
+
if (!validInstant(s)) return undefined;
|
|
112
|
+
return new Date(s).toISOString();
|
|
113
|
+
};
|
|
114
|
+
const first = instant(start);
|
|
115
|
+
const last = instant(end);
|
|
116
|
+
if (first && last) {
|
|
117
|
+
start = first;
|
|
118
|
+
end = last;
|
|
119
|
+
} else {
|
|
120
|
+
const normalized = normalizeEventDates({
|
|
121
|
+
start,
|
|
122
|
+
end,
|
|
123
|
+
allDay: false,
|
|
124
|
+
timezone,
|
|
125
|
+
});
|
|
126
|
+
if (!normalized.dates)
|
|
127
|
+
reject("Invalid local datetime or timezone.");
|
|
128
|
+
({ start, end } = normalized.dates);
|
|
129
|
+
}
|
|
130
|
+
if (Date.parse(end) < Date.parse(start))
|
|
131
|
+
reject("End must be at or after start.");
|
|
132
|
+
result.start = start;
|
|
133
|
+
result.end = end;
|
|
134
|
+
delete result.start_date;
|
|
135
|
+
delete result.end_date;
|
|
136
|
+
}
|
|
137
|
+
const recurrence = parseJson(data.recurrence);
|
|
138
|
+
if (
|
|
139
|
+
recurrence !== undefined &&
|
|
140
|
+
recurrence !== null &&
|
|
141
|
+
!validRecurrence(recurrence)
|
|
142
|
+
)
|
|
143
|
+
reject("Invalid recurrence rule.");
|
|
144
|
+
if (recurrence && validRecurrence(recurrence)) {
|
|
145
|
+
const firstDate = allDay
|
|
146
|
+
? start
|
|
147
|
+
: instantToLocalDateTime(start, timezone)?.slice(0, 10);
|
|
148
|
+
if (!firstDate || recurrence.until < firstDate)
|
|
149
|
+
reject("Recurrence until precedes start.");
|
|
150
|
+
if (
|
|
151
|
+
!allDay &&
|
|
152
|
+
(new Date(start).getUTCSeconds() || new Date(start).getUTCMilliseconds())
|
|
153
|
+
)
|
|
154
|
+
reject("Recurring starts require minute precision.");
|
|
155
|
+
}
|
|
156
|
+
const exceptions = parseJson(data.exceptions) ?? [];
|
|
157
|
+
if (!Array.isArray(exceptions) || exceptions.length > 500)
|
|
158
|
+
reject("Exceptions must be an array of at most 500 items.");
|
|
159
|
+
const allowed = new Set([
|
|
160
|
+
"start",
|
|
161
|
+
"end",
|
|
162
|
+
"allDay",
|
|
163
|
+
"timezone",
|
|
164
|
+
"locationType",
|
|
165
|
+
"virtualUrl",
|
|
166
|
+
"status",
|
|
167
|
+
"externalUrl",
|
|
168
|
+
"imageUrl",
|
|
169
|
+
"imageMediaId",
|
|
170
|
+
"categories",
|
|
171
|
+
]);
|
|
172
|
+
const record = {
|
|
173
|
+
id: "validation",
|
|
174
|
+
start,
|
|
175
|
+
end,
|
|
176
|
+
allDay,
|
|
177
|
+
timezone,
|
|
178
|
+
recurrence,
|
|
179
|
+
exceptions,
|
|
180
|
+
published: true,
|
|
181
|
+
} as EventRecord;
|
|
182
|
+
for (const ex of exceptions) {
|
|
183
|
+
if (
|
|
184
|
+
!ex ||
|
|
185
|
+
typeof ex !== "object" ||
|
|
186
|
+
typeof ex.recurrenceId !== "string" ||
|
|
187
|
+
!["cancelled", "modified"].includes(ex.status)
|
|
188
|
+
)
|
|
189
|
+
reject("Invalid exception.");
|
|
190
|
+
if (ex.status === "modified") {
|
|
191
|
+
if (
|
|
192
|
+
!ex.overrides ||
|
|
193
|
+
typeof ex.overrides !== "object" ||
|
|
194
|
+
Array.isArray(ex.overrides) ||
|
|
195
|
+
!Object.keys(ex.overrides).length
|
|
196
|
+
)
|
|
197
|
+
reject("Modified exceptions require overrides.");
|
|
198
|
+
if (Object.keys(ex.overrides).some((k) => !allowed.has(k)))
|
|
199
|
+
reject("Editorial exception overrides belong in occurrence_content.");
|
|
200
|
+
const original = scheduledOccurrence(record, ex.recurrenceId);
|
|
201
|
+
if (!original)
|
|
202
|
+
reject("Exception is not a scheduled occurrence.");
|
|
203
|
+
const effective = { ...original, ...ex.overrides };
|
|
204
|
+
if (
|
|
205
|
+
typeof effective.allDay !== "boolean" ||
|
|
206
|
+
!isValidTimeZone(effective.timezone)
|
|
207
|
+
)
|
|
208
|
+
reject("Invalid exception timezone or allDay.");
|
|
209
|
+
if (
|
|
210
|
+
effective.allDay
|
|
211
|
+
? !isDateOnly(effective.start) ||
|
|
212
|
+
!isDateOnly(effective.end) ||
|
|
213
|
+
effective.end < effective.start
|
|
214
|
+
: !Number.isFinite(Date.parse(effective.start)) ||
|
|
215
|
+
!Number.isFinite(Date.parse(effective.end)) ||
|
|
216
|
+
Date.parse(effective.end) < Date.parse(effective.start)
|
|
217
|
+
)
|
|
218
|
+
reject("Invalid exception schedule.");
|
|
219
|
+
if (
|
|
220
|
+
!effective.allDay &&
|
|
221
|
+
[effective.start, effective.end].some((value) => !validInstant(value))
|
|
222
|
+
)
|
|
223
|
+
reject("Timed exceptions require ISO instants.");
|
|
224
|
+
for (const key of [
|
|
225
|
+
"locationType",
|
|
226
|
+
"virtualUrl",
|
|
227
|
+
"status",
|
|
228
|
+
"externalUrl",
|
|
229
|
+
"imageUrl",
|
|
230
|
+
"imageMediaId",
|
|
231
|
+
])
|
|
232
|
+
if (key in ex.overrides && typeof ex.overrides[key] !== "string")
|
|
233
|
+
reject("Exception field must be text: " + key);
|
|
234
|
+
if (
|
|
235
|
+
ex.overrides.locationType !== undefined &&
|
|
236
|
+
!["physical", "virtual", "hybrid"].includes(ex.overrides.locationType)
|
|
237
|
+
)
|
|
238
|
+
reject("Invalid exception location type.");
|
|
239
|
+
if (
|
|
240
|
+
ex.overrides.status !== undefined &&
|
|
241
|
+
!["published", "cancelled", "postponed", "rescheduled"].includes(
|
|
242
|
+
ex.overrides.status,
|
|
243
|
+
)
|
|
244
|
+
)
|
|
245
|
+
reject("Invalid exception status.");
|
|
246
|
+
for (const key of ["virtualUrl", "externalUrl", "imageUrl"])
|
|
247
|
+
if (ex.overrides[key] && !safeWebUrl(ex.overrides[key]))
|
|
248
|
+
reject("Exception URLs must use HTTP(S).");
|
|
249
|
+
if (
|
|
250
|
+
ex.overrides.categories !== undefined &&
|
|
251
|
+
(!Array.isArray(ex.overrides.categories) ||
|
|
252
|
+
ex.overrides.categories.some(
|
|
253
|
+
(value: unknown) => typeof value !== "string",
|
|
254
|
+
))
|
|
255
|
+
)
|
|
256
|
+
reject("Exception categories must be an array of strings.");
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (
|
|
260
|
+
exceptions.length &&
|
|
261
|
+
(!recurrence || !exceptionIdsMatchRecurrence(record))
|
|
262
|
+
)
|
|
263
|
+
reject("Duplicate or invalid exception recurrence IDs.");
|
|
264
|
+
const editorial = parseJson(data.occurrence_content) ?? [];
|
|
265
|
+
if (
|
|
266
|
+
!Array.isArray(editorial) ||
|
|
267
|
+
editorial.length > 500 ||
|
|
268
|
+
editorial.some(
|
|
269
|
+
(ex) =>
|
|
270
|
+
!ex ||
|
|
271
|
+
typeof ex.recurrenceId !== "string" ||
|
|
272
|
+
!ex.overrides ||
|
|
273
|
+
Object.keys(ex.overrides).some(
|
|
274
|
+
(k) => !["title", "description", "location", "organizer"].includes(k),
|
|
275
|
+
),
|
|
276
|
+
)
|
|
277
|
+
)
|
|
278
|
+
reject("Invalid occurrence_content.");
|
|
279
|
+
if (
|
|
280
|
+
new Set(editorial.map((ex) => ex.recurrenceId)).size !== editorial.length ||
|
|
281
|
+
(!options.allowStaleOccurrenceCopy &&
|
|
282
|
+
editorial.some(
|
|
283
|
+
(ex) => !recurrence || !scheduledOccurrence(record, ex.recurrenceId),
|
|
284
|
+
))
|
|
285
|
+
)
|
|
286
|
+
reject("Invalid editorial recurrence IDs.");
|
|
287
|
+
for (const copy of editorial)
|
|
288
|
+
for (const [key, value] of Object.entries(copy.overrides)) {
|
|
289
|
+
if (
|
|
290
|
+
key === "description"
|
|
291
|
+
? typeof value !== "string" && !Array.isArray(value)
|
|
292
|
+
: typeof value !== "string"
|
|
293
|
+
)
|
|
294
|
+
reject("Invalid occurrence copy field: " + key);
|
|
295
|
+
}
|
|
296
|
+
if (
|
|
297
|
+
data.location_type != null &&
|
|
298
|
+
!["physical", "virtual", "hybrid"].includes(String(data.location_type))
|
|
299
|
+
)
|
|
300
|
+
reject("Invalid location type.");
|
|
301
|
+
if (
|
|
302
|
+
data.event_status != null &&
|
|
303
|
+
!["published", "cancelled", "postponed", "rescheduled"].includes(
|
|
304
|
+
String(data.event_status),
|
|
305
|
+
)
|
|
306
|
+
)
|
|
307
|
+
reject("Invalid event status.");
|
|
308
|
+
for (const key of ["virtual_url", "external_url", "image_url"])
|
|
309
|
+
if (
|
|
310
|
+
data[key] &&
|
|
311
|
+
(typeof data[key] !== "string" || !safeWebUrl(data[key] as string))
|
|
312
|
+
)
|
|
313
|
+
reject("Public URLs must use HTTP(S): " + key);
|
|
314
|
+
const organizer = parseJson(data.organizer_details);
|
|
315
|
+
if (
|
|
316
|
+
organizer != null &&
|
|
317
|
+
(typeof organizer !== "object" ||
|
|
318
|
+
Array.isArray(organizer) ||
|
|
319
|
+
["id", "name", "website", "contactUrl"].some(
|
|
320
|
+
(key) =>
|
|
321
|
+
typeof (organizer as Record<string, unknown>)[key] !== "string",
|
|
322
|
+
) ||
|
|
323
|
+
["website", "contactUrl"].some((key) => {
|
|
324
|
+
const value = (organizer as Record<string, string>)[key];
|
|
325
|
+
return value && !safeWebUrl(value);
|
|
326
|
+
}))
|
|
327
|
+
)
|
|
328
|
+
reject("Invalid organizer details.");
|
|
329
|
+
if (
|
|
330
|
+
data.previous_start_date &&
|
|
331
|
+
!isDateOnly(data.previous_start_date as string) &&
|
|
332
|
+
!validInstant(data.previous_start_date)
|
|
333
|
+
)
|
|
334
|
+
reject("Invalid previous start date.");
|
|
335
|
+
const history = parseJson(data.schedule_history);
|
|
336
|
+
if (
|
|
337
|
+
history != null &&
|
|
338
|
+
(!Array.isArray(history) ||
|
|
339
|
+
history.length > 10 ||
|
|
340
|
+
history.some(
|
|
341
|
+
(item) =>
|
|
342
|
+
!item ||
|
|
343
|
+
!validInstant(item.changedAt) ||
|
|
344
|
+
!isValidTimeZone(item.timezone) ||
|
|
345
|
+
typeof item.allDay !== "boolean" ||
|
|
346
|
+
(item.allDay
|
|
347
|
+
? !isDateOnly(item.start) ||
|
|
348
|
+
!isDateOnly(item.end) ||
|
|
349
|
+
item.end < item.start
|
|
350
|
+
: !validInstant(item.start) ||
|
|
351
|
+
!validInstant(item.end) ||
|
|
352
|
+
Date.parse(item.end) < Date.parse(item.start)),
|
|
353
|
+
))
|
|
354
|
+
)
|
|
355
|
+
reject("Invalid schedule history.");
|
|
356
|
+
result.all_day = allDay;
|
|
357
|
+
result.timezone = timezone;
|
|
358
|
+
readCategories(data.categories);
|
|
359
|
+
if (
|
|
360
|
+
data.calendar_uid != null &&
|
|
361
|
+
(typeof data.calendar_uid !== "string" ||
|
|
362
|
+
!/^[A-Za-z0-9.!~*'()%_-]+@[A-Za-z0-9.-]+$/.test(data.calendar_uid))
|
|
363
|
+
)
|
|
364
|
+
reject("Invalid calendar UID.");
|
|
365
|
+
return result;
|
|
366
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { reject } from './messages';
|
|
2
|
+
import { instantToLocalDateTime, isDateOnly } from "./date-time";
|
|
3
|
+
import type { EventRecord } from "./event";
|
|
4
|
+
import { expandEventsInDateRange, scheduledOccurrence } from "./recurrence";
|
|
5
|
+
|
|
6
|
+
export function validOccurrenceRange(from: string, through: string): boolean {
|
|
7
|
+
return isDateOnly(from) && isDateOnly(through) && from <= through &&
|
|
8
|
+
Date.parse(`${through}T00:00:00Z`) - Date.parse(`${from}T00:00:00Z`) < 366 * 86_400_000;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface InspectedOccurrence {
|
|
12
|
+
id: string;
|
|
13
|
+
recurrenceId: string | null;
|
|
14
|
+
scheduledStart: string;
|
|
15
|
+
scheduledEnd: string;
|
|
16
|
+
scheduledLocalStart: string;
|
|
17
|
+
scheduledLocalEnd: string;
|
|
18
|
+
scheduledTimezone: string;
|
|
19
|
+
start: string;
|
|
20
|
+
end: string;
|
|
21
|
+
localStart: string;
|
|
22
|
+
localEnd: string;
|
|
23
|
+
timezone: string;
|
|
24
|
+
allDay: boolean;
|
|
25
|
+
status: "scheduled" | "cancelled" | "modified";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function local(value: string, event: EventRecord): string {
|
|
29
|
+
return event.allDay ? value : instantToLocalDateTime(value, event.timezone);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Editor/agent inspection includes cancelled dates; the public expansion stays unchanged. */
|
|
33
|
+
export function inspectOccurrences(event: EventRecord, from: string, through: string, limit = 100, offset = 0) {
|
|
34
|
+
if (!validOccurrenceRange(from, through) || !Number.isInteger(limit) || limit < 1 || limit > 100 ||
|
|
35
|
+
!Number.isInteger(offset) || offset < 0) reject("Invalid occurrence range or limit.");
|
|
36
|
+
const visible = expandEventsInDateRange([{
|
|
37
|
+
...event, exceptions: event.exceptions.filter((item) => item.status !== "cancelled"),
|
|
38
|
+
}], from, through);
|
|
39
|
+
const occurrences: InspectedOccurrence[] = visible.slice(offset, offset + limit).map((effective) => {
|
|
40
|
+
const recurrenceId = event.recurrence ? effective.id.slice(event.id.length + 1) : null;
|
|
41
|
+
const scheduled = recurrenceId ? scheduledOccurrence(event, recurrenceId)! : event;
|
|
42
|
+
const exception = event.exceptions.find((item) => item.recurrenceId === recurrenceId);
|
|
43
|
+
return {
|
|
44
|
+
id: effective.id, recurrenceId,
|
|
45
|
+
scheduledStart: scheduled.start, scheduledEnd: scheduled.end,
|
|
46
|
+
scheduledLocalStart: local(scheduled.start, scheduled), scheduledLocalEnd: local(scheduled.end, scheduled),
|
|
47
|
+
scheduledTimezone: scheduled.timezone,
|
|
48
|
+
start: effective.start, end: effective.end,
|
|
49
|
+
localStart: local(effective.start, effective), localEnd: local(effective.end, effective),
|
|
50
|
+
timezone: effective.timezone, allDay: effective.allDay, status: exception?.status ?? "scheduled",
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
return { from, through, occurrences, total: visible.length, truncated: offset + occurrences.length < visible.length };
|
|
54
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
export interface PortableTextSpan {
|
|
2
|
+
_type?: string;
|
|
3
|
+
_key?: string;
|
|
4
|
+
text?: string;
|
|
5
|
+
marks?: string[];
|
|
6
|
+
}
|
|
7
|
+
export interface PortableTextMarkDef {
|
|
8
|
+
_key: string;
|
|
9
|
+
_type: string;
|
|
10
|
+
href?: string;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
}
|
|
13
|
+
export interface PortableTextBlock {
|
|
14
|
+
_type?: string;
|
|
15
|
+
_key?: string;
|
|
16
|
+
style?: string;
|
|
17
|
+
listItem?: string;
|
|
18
|
+
level?: number;
|
|
19
|
+
children?: PortableTextSpan[];
|
|
20
|
+
markDefs?: PortableTextMarkDef[];
|
|
21
|
+
[key: string]: unknown;
|
|
22
|
+
}
|
|
23
|
+
function cleanHtml(value: string): string {
|
|
24
|
+
return value
|
|
25
|
+
.replace(/<br\s*\/?>/gi, "\n")
|
|
26
|
+
.replace(/<\/p>/gi, "\n\n")
|
|
27
|
+
.replace(
|
|
28
|
+
/<\/?(?:p|div|span|a|b|strong|em|i|ul|ol|li|h[1-6]|blockquote)(?:\s[^>]*)?>/gi,
|
|
29
|
+
"",
|
|
30
|
+
)
|
|
31
|
+
.replace(/&/g, "&")
|
|
32
|
+
.replace(/</g, "<")
|
|
33
|
+
.replace(/>/g, ">")
|
|
34
|
+
.replace(/"/g, '"')
|
|
35
|
+
.replace(/'/g, "'")
|
|
36
|
+
.trim();
|
|
37
|
+
}
|
|
38
|
+
export function portableTextToPlainText(input: unknown): string {
|
|
39
|
+
if (typeof input === "string") {
|
|
40
|
+
const trimmed = input.trim();
|
|
41
|
+
if (trimmed.startsWith("[")) {
|
|
42
|
+
try {
|
|
43
|
+
const parsed = JSON.parse(trimmed);
|
|
44
|
+
if (Array.isArray(parsed)) return portableTextToPlainText(parsed);
|
|
45
|
+
} catch {}
|
|
46
|
+
}
|
|
47
|
+
return portableTextToPlainText(plainTextToPortableText(trimmed));
|
|
48
|
+
}
|
|
49
|
+
if (!Array.isArray(input)) return "";
|
|
50
|
+
const lines: string[] = [];
|
|
51
|
+
for (const value of input) {
|
|
52
|
+
if (!value || typeof value !== "object") continue;
|
|
53
|
+
const block = value as PortableTextBlock;
|
|
54
|
+
if (block._type && block._type !== "block") continue;
|
|
55
|
+
const marks = new Map<string, string>();
|
|
56
|
+
for (const mark of Array.isArray(block.markDefs) ? block.markDefs : [])
|
|
57
|
+
if (
|
|
58
|
+
mark &&
|
|
59
|
+
typeof mark._key === "string" &&
|
|
60
|
+
typeof mark.href === "string" &&
|
|
61
|
+
/^(?:https?:|mailto:)/i.test(mark.href)
|
|
62
|
+
)
|
|
63
|
+
marks.set(mark._key, mark.href);
|
|
64
|
+
const text = (Array.isArray(block.children) ? block.children : [])
|
|
65
|
+
.map((child) => {
|
|
66
|
+
if (!child || typeof child.text !== "string") return "";
|
|
67
|
+
const href = (Array.isArray(child.marks) ? child.marks : [])
|
|
68
|
+
.map((mark) => marks.get(mark))
|
|
69
|
+
.find(Boolean);
|
|
70
|
+
return (
|
|
71
|
+
child.text +
|
|
72
|
+
(href && !child.text.includes(href) ? " (" + href + ")" : "")
|
|
73
|
+
);
|
|
74
|
+
})
|
|
75
|
+
.join("")
|
|
76
|
+
.trim();
|
|
77
|
+
if (text)
|
|
78
|
+
lines.push(
|
|
79
|
+
(block.listItem === "bullet"
|
|
80
|
+
? "- "
|
|
81
|
+
: block.listItem === "number"
|
|
82
|
+
? "1. "
|
|
83
|
+
: "") + text,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
return lines.join("\n\n");
|
|
87
|
+
}
|
|
88
|
+
/** Common Markdown constructs map to editable blocks; unsupported syntax stays literal.
|
|
89
|
+
* The migration retains the original Markdown in legacy_metadata for lossless recovery.
|
|
90
|
+
*/
|
|
91
|
+
export function plainTextToPortableText(text: string): PortableTextBlock[] {
|
|
92
|
+
if (typeof text !== "string" || !text.trim()) return [];
|
|
93
|
+
const blocks: PortableTextBlock[] = [];
|
|
94
|
+
let paragraph: string[] = [];
|
|
95
|
+
const add = (
|
|
96
|
+
value: string,
|
|
97
|
+
style = "normal",
|
|
98
|
+
listItem?: string,
|
|
99
|
+
level?: number,
|
|
100
|
+
) => {
|
|
101
|
+
const key = "block_" + blocks.length;
|
|
102
|
+
const marks: PortableTextMarkDef[] = [];
|
|
103
|
+
const children: PortableTextSpan[] = [];
|
|
104
|
+
const tokens =
|
|
105
|
+
/\[([^\]]+)\]\((https?:\/\/[^\s)]+|mailto:[^\s)]+)\)|\*\*([^*]+)\*\*|__([^_]+)__|\*([^*]+)\*|_([^_]+)_|\x60([^\x60]+)\x60/g;
|
|
106
|
+
let offset = 0;
|
|
107
|
+
let match: RegExpExecArray | null;
|
|
108
|
+
const span = (value: string, spanMarks: string[] = []) => {
|
|
109
|
+
if (value)
|
|
110
|
+
children.push({
|
|
111
|
+
_type: "span",
|
|
112
|
+
_key: key + "_" + children.length,
|
|
113
|
+
text: value,
|
|
114
|
+
marks: spanMarks,
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
while ((match = tokens.exec(value))) {
|
|
118
|
+
span(value.slice(offset, match.index));
|
|
119
|
+
if (match[1] && match[2]) {
|
|
120
|
+
const markKey = key + "_link_" + marks.length;
|
|
121
|
+
marks.push({ _key: markKey, _type: "link", href: match[2] });
|
|
122
|
+
span(match[1], [markKey]);
|
|
123
|
+
} else
|
|
124
|
+
span(match[3] ?? match[4] ?? match[5] ?? match[6] ?? match[7] ?? "", [
|
|
125
|
+
match[3] || match[4] ? "strong" : match[7] ? "code" : "em",
|
|
126
|
+
]);
|
|
127
|
+
offset = match.index + match[0].length;
|
|
128
|
+
}
|
|
129
|
+
span(value.slice(offset));
|
|
130
|
+
blocks.push({
|
|
131
|
+
_type: "block",
|
|
132
|
+
_key: key,
|
|
133
|
+
style,
|
|
134
|
+
markDefs: marks,
|
|
135
|
+
children,
|
|
136
|
+
...(listItem ? { listItem, level: level ?? 1 } : {}),
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
const flush = () => {
|
|
140
|
+
if (paragraph.length) add(paragraph.join("\n"));
|
|
141
|
+
paragraph = [];
|
|
142
|
+
};
|
|
143
|
+
let fenced = false;
|
|
144
|
+
for (const line of text.replace(/\r\n?/g, "\n").split("\n")) {
|
|
145
|
+
if (/^\s*\x60\x60\x60/.test(line)) {
|
|
146
|
+
flush();
|
|
147
|
+
fenced = !fenced;
|
|
148
|
+
add(line);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (fenced) {
|
|
152
|
+
add(line);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (!line.trim()) {
|
|
156
|
+
flush();
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const heading = /^(#{1,6})\s+(.+)$/.exec(line);
|
|
160
|
+
const list = /^(\s*)(?:([-*+])|\d+\.)\s+(.+)$/.exec(line);
|
|
161
|
+
const quote = /^>\s?(.*)$/.exec(line);
|
|
162
|
+
if (heading) {
|
|
163
|
+
flush();
|
|
164
|
+
add(heading[2]!, "h" + heading[1]!.length);
|
|
165
|
+
} else if (list) {
|
|
166
|
+
flush();
|
|
167
|
+
add(
|
|
168
|
+
list[3]!,
|
|
169
|
+
"normal",
|
|
170
|
+
list[2] ? "bullet" : "number",
|
|
171
|
+
Math.floor(list[1]!.length / 2) + 1,
|
|
172
|
+
);
|
|
173
|
+
} else if (quote) {
|
|
174
|
+
flush();
|
|
175
|
+
add(quote[1]!, "blockquote");
|
|
176
|
+
} else paragraph.push(cleanHtml(line));
|
|
177
|
+
}
|
|
178
|
+
flush();
|
|
179
|
+
return blocks;
|
|
180
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CollectionSchemaInfo } from 'emdash';
|
|
2
|
+
import { referenceTarget } from './native-references';
|
|
3
|
+
|
|
4
|
+
/** Dependency evidence, not an Astro cache hint or permission to cache a route. */
|
|
5
|
+
export interface PublicFeedCache {
|
|
6
|
+
version: 1;
|
|
7
|
+
dependencies: string[];
|
|
8
|
+
generatedAt: string;
|
|
9
|
+
expiresAt: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Bounds host purge gaps, plugin settings changes and scheduled visibility. */
|
|
13
|
+
export const PUBLIC_FEED_CACHE_CEILING_MS = 15_000;
|
|
14
|
+
|
|
15
|
+
export function publicFeedCache(schema: CollectionSchemaInfo, now: number, defaultFrom: boolean): PublicFeedCache {
|
|
16
|
+
return {
|
|
17
|
+
version: 1,
|
|
18
|
+
// Collection tags cover every entry, translation, missing reference and empty list.
|
|
19
|
+
dependencies: [...new Set([schema.slug, referenceTarget(schema, 'venue'), referenceTarget(schema, 'organizer_ref'), 'emdash:settings'])],
|
|
20
|
+
generatedAt: new Date(now).toISOString(),
|
|
21
|
+
expiresAt: new Date(Math.min(now + PUBLIC_FEED_CACHE_CEILING_MS,
|
|
22
|
+
defaultFrom ? (Math.floor(now / 86_400_000) + 1) * 86_400_000 : Infinity)).toISOString(),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isDateOnly } from "./date-time";
|
|
2
|
+
import type { EventRecurrence, WeekdayName } from "./event";
|
|
3
|
+
|
|
4
|
+
export const WEEKDAYS: WeekdayName[] = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
|
|
5
|
+
|
|
6
|
+
export function validRecurrence(value: unknown): value is EventRecurrence {
|
|
7
|
+
if (!value || typeof value !== "object") return false;
|
|
8
|
+
const rule = value as Record<string, unknown>;
|
|
9
|
+
if (typeof rule.until !== "string" || !isDateOnly(rule.until)) return false;
|
|
10
|
+
if (rule.interval !== undefined && (typeof rule.interval !== "number" || !Number.isInteger(rule.interval) || rule.interval < 1 || rule.interval > 52)) return false;
|
|
11
|
+
if (rule.frequency === "daily") return rule.weekdays === undefined;
|
|
12
|
+
if (rule.frequency === "weekly") return rule.weekdays === undefined || Array.isArray(rule.weekdays) &&
|
|
13
|
+
rule.weekdays.length >= 1 && rule.weekdays.length <= 7 && new Set(rule.weekdays).size === rule.weekdays.length &&
|
|
14
|
+
rule.weekdays.every((day) => WEEKDAYS.includes(day));
|
|
15
|
+
if (rule.frequency !== "monthly" || !rule.pattern || typeof rule.pattern !== "object") return false;
|
|
16
|
+
const pattern = rule.pattern as Record<string, unknown>;
|
|
17
|
+
if (pattern.type === "dayOfMonth") return Number.isInteger(pattern.dayOfMonth) && Number(pattern.dayOfMonth) >= 1 && Number(pattern.dayOfMonth) <= 31 && (pattern.missingDayBehavior === "skip" || pattern.missingDayBehavior === "lastDay");
|
|
18
|
+
return pattern.type === "weekdayOfMonth" && WEEKDAYS.includes(pattern.weekday as WeekdayName) &&
|
|
19
|
+
(pattern.position === "last" || Number.isInteger(pattern.position) && Number(pattern.position) >= 1 && Number(pattern.position) <= 5);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function recurrenceSummary(rule: EventRecurrence): string {
|
|
23
|
+
const interval = rule.interval ?? 1;
|
|
24
|
+
if (rule.frequency === "weekly" && rule.weekdays) {
|
|
25
|
+
return `every ${interval === 1 ? "week" : `${interval} weeks`} on ${rule.weekdays.map((day) => day[0]!.toUpperCase() + day.slice(1)).join(", ")}`;
|
|
26
|
+
}
|
|
27
|
+
if (interval === 1) return rule.frequency;
|
|
28
|
+
const unit = rule.frequency === "daily" ? "days" : rule.frequency === "weekly" ? "weeks" : "months";
|
|
29
|
+
return `every ${interval} ${unit}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function normalizeWeekdays(days: WeekdayName[]): WeekdayName[] {
|
|
33
|
+
return [...days].sort((a, b) => (WEEKDAYS.indexOf(a) + 6) % 7 - (WEEKDAYS.indexOf(b) + 6) % 7);
|
|
34
|
+
}
|