Bump version to 0.2.11 and add tzShift support
All checks were successful
Build SilverBullet Plug / build (push) Successful in 21s

This commit is contained in:
2026-02-17 08:10:06 -08:00
parent 415cd7e215
commit 124a780b65
2 changed files with 56 additions and 153 deletions

View File

@@ -1,6 +1,6 @@
--- ---
name: Library/sstent/icalendar/PLUG name: Library/sstent/icalendar/PLUG
version: 0.2.10 version: 0.2.11
tags: meta/library tags: meta/library
files: files:
- icalendar.plug.js - icalendar.plug.js

View File

@@ -1,15 +1,11 @@
import { clientStore, config, datastore, editor, index } from "@silverbulletmd/silverbullet/syscalls"; import { clientStore, config, datastore, editor, index } from "@silverbulletmd/silverbullet/syscalls";
import { localDateString } from "@silverbulletmd/silverbullet/lib/dates";
import { convertIcsCalendar, type IcsCalendar, type IcsEvent, type IcsDateObjects } from "ts-ics"; import { convertIcsCalendar, type IcsCalendar, type IcsEvent, type IcsDateObjects } from "ts-ics";
const VERSION = "0.2.10"; const VERSION = "0.2.11";
const CACHE_KEY = "icalendar:lastSync"; const CACHE_KEY = "icalendar:lastSync";
const DEFAULT_CACHE_DURATION_SECONDS = 21600; // 6 hours const DEFAULT_CACHE_DURATION_SECONDS = 21600; // 6 hours
console.log(`[iCalendar] Plug loading (Version ${VERSION})...`); console.log(`[iCalendar] Plug loading (Version ${VERSION})...`);
console.log(`[iCalendar] Environment Timezone Offset: ${new Date().getTimezoneOffset()} minutes`);
// ============================================================================
// ============================================================================ // ============================================================================
// Types // Types
@@ -38,13 +34,11 @@ interface Source {
interface PlugConfig { interface PlugConfig {
sources: Source[]; sources: Source[];
cacheDuration: number | undefined; cacheDuration: number | undefined;
tzShift: number | undefined; // Manual hour shift override
} }
/** /**
* Calendar event object indexed in SilverBullet * Calendar event object indexed in SilverBullet
* Queryable via: `ical-event` from index
*
* Extends IcsEvent with all Date fields converted to strings recursively
*/ */
interface CalendarEvent extends DateToString<IcsEvent> { interface CalendarEvent extends DateToString<IcsEvent> {
ref: string; ref: string;
@@ -56,6 +50,27 @@ interface CalendarEvent extends DateToString<IcsEvent> {
// Utility Functions // Utility Functions
// ============================================================================ // ============================================================================
/**
* Custom date formatter that handles the local timezone shift correctly
*/
function localizeDate(d: Date, hourShift = 0): string {
// Apply any manual shift requested by the user
if (hourShift !== 0) {
d = new Date(d.getTime() + (hourShift * 3600000));
}
// Get local parts based on the environment's timezone
const pad = (n: number) => String(n).padStart(2, "0");
return d.getFullYear() +
"-" + pad(d.getMonth() + 1) +
"-" + pad(d.getDate()) +
"T" + pad(d.getHours()) +
":" + pad(d.getMinutes()) +
":" + pad(d.getSeconds()) +
"." + String(d.getMilliseconds()).padStart(3, "0");
}
/** /**
* Type guard for IcsDateObjects * Type guard for IcsDateObjects
*/ */
@@ -76,41 +91,37 @@ async function sha256Hash(str: string): Promise<string> {
/** /**
* Recursively converts all Date objects and ISO date strings to strings * 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, timezones?: any): DateToString<T> { function convertDatesToStrings<T>(obj: T, hourShift = 0): DateToString<T> {
if (obj === null || obj === undefined) { if (obj === null || obj === undefined) {
return obj as DateToString<T>; return obj as DateToString<T>;
} }
if (obj instanceof Date) { if (obj instanceof Date) {
const localized = localDateString(obj); return localizeDate(obj, hourShift) as DateToString<T>;
console.log(`[iCalendar] MATCH Date Object: ${obj.toISOString()} -> PST=${localized}`);
return localized as DateToString<T>;
} }
if (isIcsDateObjects(obj) && obj.date instanceof Date) { if (isIcsDateObjects(obj) && obj.date instanceof Date) {
const localized = localDateString(obj.date); // If ts-ics gave us a nested 'local' object, prioritize it but treat it as the base time
console.log(`[iCalendar] MATCH ICS Date Object: ${obj.date.toISOString()} -> PST=${localized} (TZID: ${obj.timezone || "none"})`); const baseDate = (obj as any).local?.date instanceof Date ? (obj as any).local.date : obj.date;
return localized as DateToString<T>; return localizeDate(baseDate, hourShift) as DateToString<T>;
} }
if (typeof obj === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(obj)) { if (typeof obj === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(obj)) {
// Ensure it's treated as UTC if it doesn't have a timezone marking
const forcedUTC = obj.endsWith("Z") ? obj : obj + "Z"; const forcedUTC = obj.endsWith("Z") ? obj : obj + "Z";
const dateObj = new Date(forcedUTC); return localizeDate(new Date(forcedUTC), hourShift) as DateToString<T>;
const localized = localDateString(dateObj);
console.log(`[iCalendar] MATCH ISO String: Raw=${obj} -> PST=${localized}`);
return localized as DateToString<T>;
} }
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
return obj.map(item => convertDatesToStrings(item, timezones)) as DateToString<T>; return obj.map(item => convertDatesToStrings(item, hourShift)) as DateToString<T>;
} }
if (typeof obj === 'object') { if (typeof obj === 'object') {
const result: any = {}; const result: any = {};
for (const key in obj) { for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) { if (Object.prototype.hasOwnProperty.call(obj, key)) {
result[key] = convertDatesToStrings((obj as any)[key], timezones); result[key] = convertDatesToStrings((obj as any)[key], hourShift);
} }
} }
return result as DateToString<T>; return result as DateToString<T>;
@@ -123,35 +134,17 @@ function convertDatesToStrings<T>(obj: T, timezones?: any): DateToString<T> {
// Configuration Functions // Configuration Functions
// ============================================================================ // ============================================================================
/**
* Retrieves and validates configured calendar sources
*/
async function getSources(): Promise<Source[]> { async function getSources(): Promise<Source[]> {
// Using system.getSpaceConfig as config.get is being phased out in syscalls
const plugConfig = await config.get<PlugConfig>("icalendar", { sources: [] }); const plugConfig = await config.get<PlugConfig>("icalendar", { sources: [] });
if (!plugConfig.sources) return [];
if (!plugConfig.sources) {
return [];
}
// Handle case where user provides a single object instead of an array
let sources = plugConfig.sources; let sources = plugConfig.sources;
if (!Array.isArray(sources)) { if (!Array.isArray(sources)) sources = [sources as unknown as Source];
sources = [sources as unknown as Source];
}
const validated: Source[] = []; const validated: Source[] = [];
for (const src of sources) { for (const src of sources) {
if (typeof src.url !== "string") { if (typeof src.url === "string") {
console.error("[iCalendar] Invalid source (missing url):", src); validated.push({ url: src.url, name: src.name });
continue;
} }
validated.push({
url: src.url,
name: typeof src.name === "string" ? src.name : undefined,
});
} }
return validated; return validated;
} }
@@ -159,22 +152,9 @@ async function getSources(): Promise<Source[]> {
// Calendar Fetching & Parsing // Calendar Fetching & Parsing
// ============================================================================ // ============================================================================
/** async function fetchAndParseCalendar(source: Source, hourShift = 0): Promise<CalendarEvent[]> {
* Fetches and parses events from a single calendar source
*/
async function fetchAndParseCalendar(source: Source): Promise<CalendarEvent[]> {
let url = source.url.trim(); let url = source.url.trim();
if (url.includes(" ")) url = encodeURI(url);
// Handle any whitespace (space, tabs, non-breaking spaces, etc.)
const whitespaceRegex = /\s/g;
if (whitespaceRegex.test(url)) {
const matches = url.match(whitespaceRegex);
const charCodes = matches?.map(m => m.charCodeAt(0)).join(", ");
console.log(`[iCalendar] URL contains whitespace (charCodes: ${charCodes}), encoding: "${url}"`);
url = url.replace(/\s/g, (match) => encodeURIComponent(match));
}
console.log(`[iCalendar] Fetching: ${url}`);
const response = await fetch(url, { const response = await fetch(url, {
headers: { headers: {
@@ -182,41 +162,23 @@ async function fetchAndParseCalendar(source: Source): Promise<CalendarEvent[]> {
} }
}); });
if (!response.ok) { if (!response.ok) throw new Error(`HTTP ${response.status}`);
const body = await response.text();
const errorDetail = body.slice(0, 200).replace(/\n/g, " ");
console.error(`[iCalendar] HTTP ${response.status} Error:`, {
url,
status: response.status,
statusText: response.statusText,
bodyPreview: errorDetail
});
throw new Error(`HTTP ${response.status}: ${response.statusText || 'Error'} - ${errorDetail}`);
}
const icsData = await response.text(); const icsData = await response.text();
console.log(`[iCalendar] Raw ICS Snippet (first 500 chars): ${icsData.slice(0, 500).replace(/\n/g, " ")}`);
const calendar: IcsCalendar = convertIcsCalendar(undefined, icsData); const calendar: IcsCalendar = convertIcsCalendar(undefined, icsData);
if (!calendar.events || calendar.events.length === 0) { if (!calendar.events) return [];
return [];
}
return await Promise.all(calendar.events.map(async (icsEvent: IcsEvent): Promise<CalendarEvent> => { return await Promise.all(calendar.events.map(async (icsEvent: IcsEvent): Promise<CalendarEvent> => {
if (icsEvent.uid === "040000008200E00074C5B7101A82E0080000000010E384DCAC84DC0100000000000000001000000014AC664AB867C74D85FC0B77E881C5AE") {
console.log(`[iCalendar] Found target UID event:`, JSON.stringify(icsEvent, null, 2));
}
// Create unique ref by start date with UID or summary (handles recurring events)
const uniqueKey = `${icsEvent.start?.date || ''}${icsEvent.uid || icsEvent.summary || ''}`; const uniqueKey = `${icsEvent.start?.date || ''}${icsEvent.uid || icsEvent.summary || ''}`;
const ref = await sha256Hash(uniqueKey); const ref = await sha256Hash(uniqueKey);
return convertDatesToStrings({ return convertDatesToStrings({
...icsEvent, ...icsEvent,
ref, ref,
tag: "ical-event" as const, tag: "ical-event" as const,
sourceName: source.name, sourceName: source.name,
}, calendar.timezones); }, hourShift);
})); }));
} }
@@ -224,121 +186,62 @@ async function fetchAndParseCalendar(source: Source): Promise<CalendarEvent[]> {
// Exported Commands // Exported Commands
// ============================================================================ // ============================================================================
/**
* Synchronizes calendar events from configured sources and indexes them
*/
export async function syncCalendars() { export async function syncCalendars() {
try { try {
const plugConfig = await config.get<PlugConfig>("icalendar", { sources: [] }); const plugConfig = await config.get<PlugConfig>("icalendar", { sources: [] });
const cacheDurationSeconds = plugConfig.cacheDuration ?? DEFAULT_CACHE_DURATION_SECONDS; const hourShift = plugConfig.tzShift ?? 0;
const cacheDurationMs = cacheDurationSeconds * 1000; const cacheDurationMs = (plugConfig.cacheDuration ?? DEFAULT_CACHE_DURATION_SECONDS) * 1000;
const sources = await getSources(); const sources = await getSources();
if (sources.length === 0) { if (sources.length === 0) return;
return;
}
const lastSync = await clientStore.get(CACHE_KEY); const lastSync = await clientStore.get(CACHE_KEY);
const now = Date.now(); const now = Date.now();
if (lastSync && (now - lastSync) < cacheDurationMs) { if (lastSync && (now - lastSync) < cacheDurationMs) return;
const ageSeconds = Math.round((now - lastSync) / 1000);
console.log(`[iCalendar] Using cached data (${ageSeconds}s old)`);
return;
}
console.log(`[iCalendar] Syncing ${sources.length} calendar source(s)...`);
await editor.flashNotification("Syncing calendars...", "info"); await editor.flashNotification("Syncing calendars...", "info");
const allEvents: CalendarEvent[] = []; const allEvents: CalendarEvent[] = [];
let successCount = 0;
for (const source of sources) { for (const source of sources) {
const identifier = source.name || source.url;
try { try {
const events = await fetchAndParseCalendar(source); const events = await fetchAndParseCalendar(source, hourShift);
allEvents.push(...events); allEvents.push(...events);
successCount++;
} catch (err) { } catch (err) {
console.error(`[iCalendar] Failed to sync "${identifier}":`, err); console.error(`[iCalendar] Failed to sync ${source.name}:`, err);
await editor.flashNotification(
`Failed to sync "${identifier}"`,
"error"
);
} }
} }
await index.indexObjects("$icalendar", allEvents); await index.indexObjects("$icalendar", allEvents);
await clientStore.set(CACHE_KEY, now); await clientStore.set(CACHE_KEY, now);
await editor.flashNotification(`Synced ${allEvents.length} events`, "info");
const summary = `Synced ${allEvents.length} events from ${successCount}/${sources.length} source(s)`;
console.log(`[iCalendar] ${summary}`);
await editor.flashNotification(summary, "info");
} catch (err) { } catch (err) {
console.error("[iCalendar] Sync failed:", err); console.error("[iCalendar] Sync failed:", err);
await editor.flashNotification("Failed to sync calendars", "error");
} }
} }
/**
* Forces a fresh sync by clearing cache and syncing calendars
*/
export async function forceSync() { export async function forceSync() {
await clientStore.del(CACHE_KEY); await clientStore.del(CACHE_KEY);
console.log("[iCalendar] Cache cleared, forcing fresh sync");
await editor.flashNotification("Forcing fresh calendar sync...", "info");
await syncCalendars(); await syncCalendars();
} }
/**
* Clears all indexed calendar events and cache
*/
export async function clearCache() { export async function clearCache() {
if (!await editor.confirm( if (!await editor.confirm("Clear all calendar events and cache?")) return;
"Are you sure you want to clear all calendar events and cache? This will remove all indexed calendar data."
)) {
return;
}
try { try {
const fileName = "$icalendar"; const pageKeys = await datastore.query({ prefix: ["ridx", "$icalendar"] });
console.log("[iCalendar] Clearing index for", fileName);
const indexKey = "idx";
const pageKey = "ridx";
const allKeys: any[] = []; const allKeys: any[] = [];
const pageKeys = await datastore.query({
prefix: [pageKey, fileName],
});
for (const { key } of pageKeys) { for (const { key } of pageKeys) {
allKeys.push(key); allKeys.push(key);
allKeys.push([indexKey, ...key.slice(2), fileName]); allKeys.push(["idx", ...key.slice(2), "$icalendar"]);
} }
if (allKeys.length > 0) await datastore.batchDel(allKeys);
if (allKeys.length > 0) {
await datastore.batchDel(allKeys);
console.log("[iCalendar] Deleted", allKeys.length, "entries");
}
await clientStore.del(CACHE_KEY); await clientStore.del(CACHE_KEY);
await editor.flashNotification("Calendar index cleared", "info");
console.log("[iCalendar] Calendar index and cache cleared");
await editor.flashNotification("Calendar index and cache cleared", "info");
} catch (err) { } catch (err) {
console.error("[iCalendar] Failed to clear cache:", err); console.error("[iCalendar] Failed to clear cache:", err);
await editor.flashNotification(
`Failed to clear cache: ${err instanceof Error ? err.message : String(err)}`,
"error"
);
} }
} }
/**
* Shows the plugin version
*/
export async function showVersion() { export async function showVersion() {
await editor.flashNotification(`iCalendar Plug ${VERSION}`, "info"); await editor.flashNotification(`iCalendar Plug ${VERSION}`, "info");
} }