Fix: Restore proven recursive date sanitization and unique indexing from deb30ab and iterate to 0.3.25
All checks were successful
Build SilverBullet Plug / build (push) Successful in 30s

This commit is contained in:
2026-02-18 10:26:50 -08:00
parent 3c69a3567b
commit 9b54e2d8a8
6 changed files with 95 additions and 52 deletions

View File

@@ -1,6 +1,6 @@
---
name: Library/sstent/icalendar
version: "0.3.24"
version: "0.3.25"
tags: meta/library
files:
- icalendar.plug.js

View File

@@ -1,6 +1,6 @@
{
"name": "icalendar-plug",
"version": "0.3.24",
"version": "0.3.25",
"nodeModulesDir": "auto",
"tasks": {
"sync-version": "deno run -A scripts/sync-version.ts",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,5 @@
name: icalendar
version: 0.3.24
version: 0.3.25
author: sstent
index: icalendar.ts
# Legacy SilverBullet permission name

View File

@@ -1,7 +1,7 @@
import { clientStore, config, datastore, editor, index } from "@silverbulletmd/silverbullet/syscalls";
import { convertIcsCalendar } from "https://esm.sh/ts-ics@2.4.0";
const VERSION = "0.3.24";
const VERSION = "0.3.25";
const CACHE_KEY = "icalendar:lastSync";
console.log(`[iCalendar] Plug script executing at top level (Version ${VERSION})`);
@@ -20,6 +20,72 @@ const TIMEZONE_OFFSETS: Record<string, number> = {
"None": 0
};
// ============================================================================
// Utility Functions
// ============================================================================
/**
* Creates a SHA-256 hash of a string (hex encoded)
*/
async function sha256Hash(str: string): Promise<string> {
const encoder = new TextEncoder();
const data = encoder.encode(str);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
}
function localDateString(date: Date): string {
const pad = (n: number) => String(n).padStart(2, "0");
return date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate()) + "T" + pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds());
}
/**
* Recursively converts all Date objects and ISO date strings to strings
* Handles nested objects like {date: Date, local: {date: Date, timezone: string}}
*/
function convertDatesToStrings<T>(obj: T): any {
if (obj === null || obj === undefined) {
return obj;
}
if (obj instanceof Date) {
return localDateString(obj);
}
if (typeof obj === 'object' && 'date' in obj && (obj as any).date instanceof Date) {
return localDateString((obj as any).date);
}
if (typeof obj === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(obj)) {
try {
return localDateString(new Date(obj));
} catch {
return obj;
}
}
if (Array.isArray(obj)) {
return obj.map(item => convertDatesToStrings(item));
}
if (typeof obj === 'object') {
const result: any = {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
result[key] = convertDatesToStrings((obj as any)[key]);
}
}
return result;
}
return obj;
}
// ============================================================================
// Configuration Functions
// ============================================================================
async function getSources(): Promise<{ sources: any[], tzShift: number }> {
try {
const rawConfig = await config.get("icalendar", { sources: [] });
@@ -28,13 +94,10 @@ async function getSources(): Promise<{ sources: any[], tzShift: number }> {
let sources = rawConfig.sources || [];
let tzShift = rawConfig.tzShift || 0;
// Handle common Space Lua structure where tzShift might be inside the sources table
if (sources && typeof sources === "object" && !Array.isArray(sources)) {
if (sources.tzShift !== undefined && tzShift === 0) {
tzShift = sources.tzShift;
console.log("[iCalendar] Found tzShift inside sources object:", tzShift);
}
// Convert map-like sources to array
const sourceArray = [];
for (const key in sources) {
if (sources[key] && typeof sources[key].url === "string") {
@@ -44,7 +107,6 @@ async function getSources(): Promise<{ sources: any[], tzShift: number }> {
sources = sourceArray;
}
console.log(`[iCalendar] Final Sources: ${sources.length}, tzShift: ${tzShift}`);
return { sources, tzShift };
} catch (e) {
console.error("[iCalendar] Error in getSources:", e);
@@ -52,6 +114,10 @@ async function getSources(): Promise<{ sources: any[], tzShift: number }> {
}
}
// ============================================================================
// Calendar Fetching & Parsing
// ============================================================================
async function fetchAndParseCalendar(source: any, hourShift = 0): Promise<any[]> {
console.log(`[iCalendar] Fetching from: ${source.url}`);
try {
@@ -61,38 +127,24 @@ async function fetchAndParseCalendar(source: any, hourShift = 0): Promise<any[]>
return [];
}
const text = await response.text();
console.log(`[iCalendar] Received ICS data (${text.length} chars) from ${source.name}`);
// Log a snippet of the ICS to verify content
console.log(`[iCalendar] ICS snippet: ${text.substring(0, 200).replace(/\n/g, "\\n")}`);
const calendar = convertIcsCalendar(undefined, text);
if (!calendar || !calendar.events) {
console.warn(`[iCalendar] No events found in ICS from ${source.name} using ts-ics`);
return [];
}
console.log(`[iCalendar] ts-ics found ${calendar.events.length} events in ${source.name}`);
const events: any[] = [];
for (const icsEvent of calendar.events) {
const obj = icsEvent.start;
if (!obj) {
console.warn("[iCalendar] Event has no start object:", icsEvent.summary);
continue;
}
if (!obj) continue;
let wallTimeStr = "";
if (obj.local && obj.local.date) {
wallTimeStr = typeof obj.local.date === "string" ? obj.local.date : obj.local.date.toISOString?.() || String(obj.local.date);
wallTimeStr = typeof obj.local.date === "string" ? obj.local.date : (obj.local.date instanceof Date ? obj.local.date.toISOString() : String(obj.local.date));
} else if (obj.date) {
wallTimeStr = typeof obj.date === "string" ? obj.date : obj.date.toISOString?.() || String(obj.date);
wallTimeStr = typeof obj.date === "string" ? obj.date : (obj.date instanceof Date ? obj.date.toISOString() : String(obj.date));
}
if (!wallTimeStr) {
console.warn("[iCalendar] Skipping event with no start date string:", icsEvent.summary, "Start object structure:", JSON.stringify(obj), "typeof date:", typeof obj.date, "typeof local.date:", typeof obj.local?.date);
continue;
}
if (!wallTimeStr) continue;
const baseDate = new Date(wallTimeStr.replace("Z", "") + "Z");
const tzName = obj.local?.timezone || obj.timezone || "UTC";
@@ -100,22 +152,18 @@ async function fetchAndParseCalendar(source: any, hourShift = 0): Promise<any[]>
const utcMillis = baseDate.getTime() - (sourceOffset * 3600000);
const finalDate = new Date(utcMillis + (hourShift * 3600000));
const pad = (n: number) => String(n).padStart(2, "0");
const localIso = finalDate.getFullYear() + "-" + pad(finalDate.getMonth() + 1) + "-" + pad(finalDate.getDate()) + "T" + pad(finalDate.getHours()) + ":" + pad(finalDate.getMinutes()) + ":" + pad(finalDate.getSeconds());
const localIso = localDateString(finalDate);
const uniqueKey = `${localIso}${icsEvent.uid || icsEvent.summary || ''}`;
const ref = await sha256Hash(uniqueKey);
// Picking only essential fields and ensuring they are strings to avoid indexing issues
events.push({
events.push(convertDatesToStrings({
...icsEvent,
name: icsEvent.summary || "Untitled Event",
start: localIso,
location: typeof icsEvent.location === "string" ? icsEvent.location : (icsEvent.location?.value || ""),
description: typeof icsEvent.description === "string" ? icsEvent.description : (icsEvent.description?.value || ""),
url: icsEvent.url || "",
ref,
tag: "ical-event",
sourceName: source.name
});
}
if (events.length > 0) {
console.log(`[iCalendar] Sample event from ${source.name}:`, JSON.stringify(events[0]));
}));
}
return events;
} catch (err) {
@@ -125,20 +173,16 @@ async function fetchAndParseCalendar(source: any, hourShift = 0): Promise<any[]>
}
export async function syncCalendars() {
console.log("[iCalendar] syncCalendars() started");
try {
const { sources, tzShift } = await getSources();
if (sources.length === 0) {
console.log("[iCalendar] No sources to sync");
return;
}
if (sources.length === 0) return;
await editor.flashNotification("Syncing calendars...", "info");
const allEvents: any[] = [];
for (const source of sources) {
const events = await fetchAndParseCalendar(source, tzShift);
allEvents.push(...events);
}
console.log(`[iCalendar] Total events to index: ${allEvents.length}`);
await index.indexObjects("$icalendar", allEvents);
await editor.flashNotification(`Synced ${allEvents.length} events`, "info");
} catch (err) {
@@ -147,7 +191,6 @@ export async function syncCalendars() {
}
export async function forceSync() {
console.log("[iCalendar] forceSync() triggered");
await clientStore.del(CACHE_KEY);
await syncCalendars();
}
@@ -167,4 +210,4 @@ export async function clearCache() {
export async function showVersion() {
await editor.flashNotification(`iCalendar Plug ${VERSION}`, "info");
}
}