forked from GitHubMirrors/silverbullet-icalendar
Compare commits
2 Commits
2ea763e145
...
6480b56875
| Author | SHA1 | Date | |
|---|---|---|---|
| 6480b56875 | |||
| d28c206862 |
34
Makefile
Normal file
34
Makefile
Normal file
@@ -0,0 +1,34 @@
|
||||
.PHONY: build up down logs clean
|
||||
|
||||
# Build the plug using a Docker container with Deno
|
||||
build:
|
||||
docker run --rm -v $(PWD):/app -w /app denoland/deno:latest task build
|
||||
|
||||
# Start the SilverBullet test container
|
||||
up:
|
||||
mkdir -p test_space
|
||||
docker compose up -d
|
||||
|
||||
# Stop the SilverBullet test container
|
||||
down:
|
||||
docker compose down
|
||||
|
||||
# View logs from the SilverBullet container
|
||||
logs:
|
||||
docker compose logs -f
|
||||
|
||||
# Watch for changes and rebuild automatically using Deno's internal watch
|
||||
watch:
|
||||
@echo "Starting watch in background..."
|
||||
docker run -d --name ical-watch -v $(PWD):/app -w /app denoland/deno:latest task watch
|
||||
@echo "Watching... Use 'docker logs -f ical-watch' to see build progress."
|
||||
|
||||
# Stop the watch container
|
||||
stop-watch:
|
||||
docker rm -f ical-watch
|
||||
|
||||
# Clean up build artifacts and test space
|
||||
clean:
|
||||
rm -f *.plug.js
|
||||
# Be careful with test_space if you have notes there you want to keep
|
||||
# rm -rf test_space
|
||||
10
docker-compose.yml
Normal file
10
docker-compose.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
services:
|
||||
silverbullet:
|
||||
image: zefhemel/silverbullet:latest
|
||||
ports:
|
||||
- "3000:3000"
|
||||
volumes:
|
||||
- ./test_space:/space
|
||||
- ./icalendar.plug.js:/space/_plug/icalendar.plug.js:ro
|
||||
environment:
|
||||
- SB_USER=admin:admin # Default for easy local testing
|
||||
File diff suppressed because one or more lines are too long
58
icalendar.ts
58
icalendar.ts
@@ -1,10 +1,25 @@
|
||||
import { clientStore, config, datastore, editor, index } from "@silverbulletmd/silverbullet/syscalls";
|
||||
import { convertIcsCalendar, type IcsCalendar, type IcsEvent, type IcsDateObjects } from "ts-ics";
|
||||
|
||||
const VERSION = "0.2.14";
|
||||
const VERSION = "0.2.15";
|
||||
const CACHE_KEY = "icalendar:lastSync";
|
||||
const DEFAULT_CACHE_DURATION_SECONDS = 21600; // 6 hours
|
||||
|
||||
// Mapping of common Windows/Outlook timezones to their standard offsets (in hours)
|
||||
const TIMEZONE_OFFSETS: Record<string, number> = {
|
||||
"GMT Standard Time": 0,
|
||||
"W. Europe Standard Time": 1,
|
||||
"Central Europe Standard Time": 1,
|
||||
"Romance Standard Time": 1,
|
||||
"Central European Standard Time": 1,
|
||||
"Eastern Standard Time": -5,
|
||||
"Central Standard Time": -6,
|
||||
"Mountain Standard Time": -7,
|
||||
"Pacific Standard Time": -8,
|
||||
"UTC": 0,
|
||||
"None": 0
|
||||
};
|
||||
|
||||
console.log(`[iCalendar] Plug loading (Version ${VERSION})...`);
|
||||
|
||||
// ============================================================================
|
||||
@@ -53,21 +68,42 @@ function toLocalISO(d: Date): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* Simplified date processor that trusts the parsed Date object
|
||||
* Robustly converts an ICS date object to a localized PST string
|
||||
*/
|
||||
function processIcsDate(obj: any, manualShift = 0): string {
|
||||
if (!obj) return "";
|
||||
|
||||
// The 'date' property from ts-ics is already a native JS Date object
|
||||
// which has been parsed with the correct TZID context if available.
|
||||
const d = obj.date instanceof Date ? obj.date : (obj instanceof Date ? obj : null);
|
||||
|
||||
if (!d) return "";
|
||||
// 1. Get the "Wall Time" (the hour shown in the organizer's calendar)
|
||||
// ts-ics often puts this in obj.local.date but marks it with 'Z'
|
||||
let wallTimeStr = (obj.local && typeof obj.local.date === "string")
|
||||
? obj.local.date
|
||||
: (typeof obj.date === "string" ? obj.date : "");
|
||||
|
||||
if (!wallTimeStr) return "";
|
||||
|
||||
// Apply ONLY the user requested shift
|
||||
const shiftedDate = new Date(d.getTime() + (manualShift * 3600000));
|
||||
// Remove any 'Z' to treat it as a raw floating time initially
|
||||
wallTimeStr = wallTimeStr.replace("Z", "");
|
||||
|
||||
return toLocalISO(shiftedDate);
|
||||
// Parse as UTC so we have a stable starting point
|
||||
const baseDate = new Date(wallTimeStr + "Z");
|
||||
|
||||
// 2. Identify the Source Timezone
|
||||
const tzName = obj.local?.timezone || obj.timezone || "UTC";
|
||||
const sourceOffset = TIMEZONE_OFFSETS[tzName] ?? 0;
|
||||
|
||||
// 3. Calculate True UTC
|
||||
// UTC = WallTime - SourceOffset
|
||||
// Example: 16:00 WallTime in GMT+1 (+1) -> 15:00 UTC
|
||||
const utcMillis = baseDate.getTime() - (sourceOffset * 3600000);
|
||||
|
||||
const envOffset = new Date().getTimezoneOffset();
|
||||
console.log(`[iCalendar] Date Calc: Wall=${wallTimeStr}, TZ=${tzName}, SourceOffset=${sourceOffset}, EnvOffset=${envOffset}, UTC=${new Date(utcMillis).toISOString()}`);
|
||||
|
||||
// 4. Apply User's Manual Shift (if any)
|
||||
const finalMillis = utcMillis + (manualShift * 3600000);
|
||||
|
||||
// 5. Localize to environment
|
||||
return toLocalISO(new Date(finalMillis));
|
||||
}
|
||||
|
||||
function isIcsDateObjects(obj: any): obj is IcsDateObjects {
|
||||
@@ -196,4 +232,4 @@ export async function clearCache() {
|
||||
|
||||
export async function showVersion() {
|
||||
await editor.flashNotification(`iCalendar Plug ${VERSION}`, "info");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user