Public: anyone can read this; the crew can change it.
An .ics calendar file opens as its events in the browser #13615
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13591-an-ics-calendar-file-opens-as-its-events → main 3 commits fork
3 files
changed +17
−27
docs/features/ics.md +3 −3
| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | # Calendar files (.ics) as a list of events |
| 2 | 2 | |
| 3 | Open a `.ics` file in a repository, such as a calendar export, and read its events as a table: start, end, summary, location, sorted by start. | |
| 3 | Open a `.ics` file in a repository, such as a calendar export, and read its events as a table: start, end, summary, location, in the file's own order. | |
| 4 | 4 | |
| 5 | 5 | - **Web:** the blob page draws the table on the server (`src/web/render/ics.ts`, `parseIcs`). `?view=source`, Raw and Download work as for any file. |
| 6 | 6 | - **Terminal:** the workspace opens a `.ics` as the same table (`src/tui/screens/workspace.ts`, sharing `parseIcs` and `EVENT_CAP`); `s` flips to the source. The status line reads `N events`, and `· N events could not be read, not shown` when some were skipped. |
| 7 | 7 | - Folded lines are unfolded and `\,` `\;` `\n` escapes are undone. UTC times show as `UTC`, `TZID` times keep their zone name, all-day events show dates and the last day (the file's end is exclusive). |
| 8 | - Events are sorted by start as written (text order); time zones are not applied, so events in different zones can be out of order, and the web says so in one line when a file holds more than one zone. | |
| 8 | - Events appear in the order the file lists them; times are shown as written, with their zone name, and no zone is applied. | |
| 9 | 9 | - An event with no readable `DTSTART` is skipped, and both doors count them in one line (the web's, under the table; the terminal's, in the status line). |
| 10 | - The first 200 events (`EVENT_CAP`) are shown; the rest are counted. | |
| 10 | - The first 500 events (`EVENT_CAP`) are shown; the rest are counted. | |
| 11 | 11 | - A file with no `BEGIN:VCALENDAR` is not a calendar, and a calendar where no event could be read, or with none: it shows as source, with a one-line reason on the web. |
| 12 | 12 | - All text is escaped on output; the parser is pure and never throws. |
src/web/render/ics.ts +3 −11
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * A calendar file (iCalendar `.ics`, RFC 5545) as a list of its events: start, |
| 3 | * end (or "all day"), summary and location, sorted by start. Lines are unfolded | |
| 3 | * end (or "all day"), summary and location, in the file's own order. Lines are unfolded | |
| 4 | 4 | * (§3.1), VEVENT blocks read, DTSTART/DTEND taken with or without TZID, and |
| 5 | 5 | * TEXT escapes (`\,` `\;` `\\` `\n`) undone. Every value is escaped on output. |
| 6 | 6 | * A file that is not a calendar falls back to its source; an event that cannot |
| @@ -12,7 +12,7 @@ import { fileSize } from "../ui.ts"; | ||
| 12 | 12 | import type { Rendered, RenderInput } from "./renderers.ts"; |
| 13 | 13 | |
| 14 | 14 | /** Events drawn, whatever the file holds. */ |
| 15 | export const EVENT_CAP = 200; | |
| 15 | export const EVENT_CAP = 500; | |
| 16 | 16 | |
| 17 | 17 | export interface IcsEvent { |
| 18 | 18 | /** `2026-03-01 09:00`, `2026-03-01`, with a zone note when the file gave one. */ |
| @@ -133,8 +133,6 @@ export function parseIcs(source: string): Calendar { | ||
| 133 | 133 | } |
| 134 | 134 | } |
| 135 | 135 | if (props !== undefined) skipped++; |
| 136 | // Stable: events that start together keep file order. Dates sort as text; a zone is not applied. | |
| 137 | events.sort((a, b) => (a.start < b.start ? -1 : a.start > b.start ? 1 : 0)); | |
| 138 | 136 | return { events, skipped }; |
| 139 | 137 | } |
| 140 | 138 | |
| @@ -179,12 +177,6 @@ export function ics(input: RenderInput): Rendered { | ||
| 179 | 177 | cal.skipped > 0 |
| 180 | 178 | ? `<p class="muted small">${cal.skipped.toLocaleString()} ${cal.skipped === 1 ? "event" : "events"} could not be read and ${cal.skipped === 1 ? "is" : "are"} not shown.</p>` |
| 181 | 179 | : ""; |
| 182 | // Start times sort as written, so two zones can be out of order against each other: say so. | |
| 183 | const zones = new Set(cal.events.filter((e) => !e.allDay).map((e) => e.start.slice(17))); | |
| 184 | const zoned = | |
| 185 | zones.size > 1 | |
| 186 | ? `<p class="muted small">Events are sorted by the start as written; time zones are not applied, so events in different zones may be out of order.</p>` | |
| 187 | : ""; | |
| 188 | 180 | const more = |
| 189 | 181 | cal.events.length > EVENT_CAP |
| 190 | 182 | ? `<p class="muted small">Showing ${EVENT_CAP.toLocaleString()} of ${cal.events.length.toLocaleString()} events, ${(cal.events.length - EVENT_CAP).toLocaleString()} more. The rest is in the raw file.</p>` |
| @@ -193,7 +185,7 @@ export function ics(input: RenderInput): Rendered { | ||
| 193 | 185 | const content = html`<table class="table" data-dense aria-label="${name} events"> |
| 194 | 186 | <thead><tr><th scope="col">Start</th><th scope="col">End</th><th scope="col">Summary</th><th scope="col">Location</th></tr></thead> |
| 195 | 187 | <tbody>${raw(rows)}</tbody> |
| 196 | </table>${raw(more)}${raw(bad)}${raw(zoned)}`; | |
| 188 | </table>${raw(more)}${raw(bad)}`; | |
| 197 | 189 | const n = cal.events.length; |
| 198 | 190 | return { content, lines: 0, summary: `${n.toLocaleString()} ${n === 1 ? "event" : "events"} · ${fileSize(input.size)}` }; |
| 199 | 191 | } |
test/render-ics.test.ts +11 −13
| @@ -15,7 +15,7 @@ describe("calendar files", () => { | ||
| 15 | 15 | expect(RENDERERS.calendar).toBe(ics); |
| 16 | 16 | }); |
| 17 | 17 | |
| 18 | test("folded lines, escapes, UTC and TZID times, sorted by start", () => { | |
| 18 | test("folded lines, escapes, UTC and TZID times", () => { | |
| 19 | 19 | const cal = parseIcs( |
| 20 | 20 | wrap( |
| 21 | 21 | "DTSTART:20260301T090000Z\r\nDTEND:20260301T100000Z\r\nSUMMARY:Plan\\, build\\nand a very long\r\n title folded\r\nLOCATION:Room 1\\; East", |
| @@ -40,8 +40,8 @@ describe("calendar files", () => { | ||
| 40 | 40 | ); |
| 41 | 41 | if ("error" in cal) throw new Error(cal.error); |
| 42 | 42 | expect(cal.events.map((e) => [e.allDay, e.start, e.end])).toEqual([ |
| 43 | [true, "2026-02-28", "2026-03-02"], | |
| 44 | 43 | [true, "2026-03-01", ""], |
| 44 | [true, "2026-02-28", "2026-03-02"], | |
| 45 | 45 | ]); |
| 46 | 46 | const out = String(draw(wrap("DTSTART;VALUE=DATE:20260301\r\nSUMMARY:X")).content); |
| 47 | 47 | expect(out).toContain("all day"); |
| @@ -62,14 +62,14 @@ describe("calendar files", () => { | ||
| 62 | 62 | expect(r.summary).toContain("1 event"); |
| 63 | 63 | }); |
| 64 | 64 | |
| 65 | test("events are sorted by start, not file order; non-VEVENT parts are ignored", () => { | |
| 65 | test("events keep the file's own order; non-VEVENT parts are ignored", () => { | |
| 66 | 66 | const cal = parseIcs( |
| 67 | 67 | "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nDTSTART:20250101T000000Z\r\nSUMMARY:todo\r\nEND:VTODO\r\n" + |
| 68 | 68 | "BEGIN:VEVENT\r\nDTSTART:20260305T090000Z\r\nSUMMARY:late\r\nEND:VEVENT\r\n" + |
| 69 | 69 | "BEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20260301\r\nSUMMARY:early\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", |
| 70 | 70 | ); |
| 71 | 71 | if ("error" in cal) throw new Error(cal.error); |
| 72 | expect(cal.events.map((e) => e.summary)).toEqual(["early", "late"]); | |
| 72 | expect(cal.events.map((e) => e.summary)).toEqual(["late", "early"]); | |
| 73 | 73 | }); |
| 74 | 74 | |
| 75 | 75 | test("a malformed file shows the rows it could read and one line about the rest", () => { |
| @@ -85,14 +85,12 @@ describe("calendar files", () => { | ||
| 85 | 85 | expect(String(open.content)).toContain("1 event could not be read"); |
| 86 | 86 | }); |
| 87 | 87 | |
| 88 | test("two zones in one file get one note after the table; one zone gets none", () => { | |
| 89 | const mixed = String( | |
| 88 | test("events in different zones stay in file order, with no note", () => { | |
| 89 | const out = String( | |
| 90 | 90 | draw(wrap("DTSTART:20260301T090000Z\r\nSUMMARY:a", "DTSTART;TZID=Europe/Paris:20260301T080000\r\nSUMMARY:b")).content, |
| 91 | 91 | ); |
| 92 | expect(mixed.match(/time zones are not applied/g)?.length).toBe(1); | |
| 93 | expect(mixed.indexOf("time zones are not applied")).toBeGreaterThan(mixed.indexOf("</table>")); | |
| 94 | const one = String(draw(wrap("DTSTART:20260301T090000Z\r\nSUMMARY:a", "DTSTART:20260302T090000Z\r\nSUMMARY:b")).content); | |
| 95 | expect(one).not.toContain("time zones"); | |
| 92 | expect(out).not.toContain("time zones"); | |
| 93 | expect(out.indexOf(">a<")).toBeLessThan(out.indexOf(">b<")); | |
| 96 | 94 | }); |
| 97 | 95 | |
| 98 | 96 | test("malformed files fall back to text", () => { |
| @@ -103,11 +101,11 @@ describe("calendar files", () => { | ||
| 103 | 101 | expect(draw("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n").fallback).toContain("no events"); |
| 104 | 102 | }); |
| 105 | 103 | |
| 106 | test("capped at 200 events, saying how many more", () => { | |
| 104 | test("capped at 500 events, saying how many more", () => { | |
| 107 | 105 | const many = Array.from({ length: EVENT_CAP + 7 }, (_, i) => `DTSTART:20260301T090000Z\r\nSUMMARY:e${i}`); |
| 108 | 106 | const out = String(draw(wrap(...many)).content); |
| 109 | expect(out).toContain("e199<"); | |
| 110 | expect(out).not.toContain("e200<"); | |
| 107 | expect(out).toContain("e499<"); | |
| 108 | expect(out).not.toContain("e500<"); | |
| 111 | 109 | expect(out).toContain("7 more"); |
| 112 | 110 | }); |
| 113 | 111 | }); |
new above
offline