Public: anyone can read this; the crew can change it.

A .vcf contacts file opens as rows: name, phone, email, organisation #13091

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/13073-a-vcf-contacts-file-opens-as-rows-name-p → main 5 commits fork 3 threads · 3 unresolved

  • Merged. mason/13073-a-vcf-contacts-file-opens-as-rows-name-p was merged into main as 32f8f8b.

Review

Sign in to review this change.
7 files changed +65 −5
docs/features/vcf-view.md +1 −1
@@ -2,7 +2,7 @@
22
33 Open a `.vcf` or `.vcard` file in a repository and read it as a list of its contacts, in file order, from a card's name and organisation to its email, phone, web and note.
44
5 - **Web:** the blob page draws one row per person, with the labelled facts folded under the name so it fits a phone (`src/web/render/vcf.ts`). Values are escaped and urls are text, never links. Files over 5 MB, or with no complete card, show as source; past 500 contacts a "Showing first N" line says how many were left out. `?view=source`, Raw and Download work as for any file.
5 - **Web:** the blob page draws one row per person, with the labelled facts folded under the name so it fits a phone (`src/web/render/vcf.ts`). Values are escaped and urls are text, never links. Files over 1 MB, or with no `BEGIN:VCARD`, show as source; a card without `END:VCARD` shows as a muted "Malformed card" row, counted, and its fields are left out; past 500 contacts a "Showing first N" line says how many were left out. `?view=source`, Raw and Download work as for any file.
66 - **Terminal:** the workspace opens the same file as a table of name, organisation, email and phone, sharing the parser; `s` flips to the source. The status line reads `N contacts`.
77
88 Not done: vCard 2.1 quoted-printable and photos.
src/tui/screens/workspace.ts +1 −1
@@ -394,7 +394,7 @@ function vcfTable(source: string): Table | null {
394394 const shown = parsed.cards.slice(0, CARD_CAP);
395395 return {
396396 header: ["Name", "Organisation", "Email", "Phone"],
397 rows: shown.map((c) => [c.name, c.org, c.emails.join(", "), c.phones.join(", ")]),
397 rows: shown.map((c) => c.malformed ? [`(${c.name})`, "—", "—", "—"] : [c.name, c.org, c.emails.join(", "), c.phones.join(", ")]),
398398 more: parsed.total - shown.length,
399399 delimiter: ",",
400400 columns: 4,
src/web/render/kinds.ts +1 −1
@@ -107,7 +107,7 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = {
107107 otio: 1 * MB,
108108 edl: 1 * MB,
109109 fcpxml: 1 * MB,
110 contacts: 5 * MB,
110 contacts: 1 * MB,
111111 mail: 5 * MB,
112112 gcode: 50 * MB,
113113 dxf: 5 * MB,
src/web/render/vcf-parse.ts +9 −1
@@ -22,6 +22,8 @@ export interface Card {
2222 readonly phones: string[];
2323 readonly urls: string[];
2424 readonly note: string;
25 /** The card never closed with END:VCARD; its fields are not trusted. */

Nothing reads malformed. Use it in vcf.ts row() to render the row as muted text, not as a contact name, and in the TUI table.

26 readonly malformed?: true;
2527 }
2628
2729 export type Contacts = { readonly cards: Card[]; readonly total: number } | { readonly error: string };
@@ -51,9 +53,14 @@ export function parseVcf(source: string): Contacts {
5153 const cards: Card[] = [];
5254 let total = 0;
5355 let cur: { fn: string; n: string; org: string; emails: string[]; phones: string[]; urls: string[]; note: string } | undefined;
56 const keepBroken = () => {
57 total++;
58 if (cards.length < CARD_CAP) cards.push({ name: "Malformed card: no END:VCARD", org: "", emails: [], phones: [], urls: [], note: "", malformed: true });
59 };
5460 for (const line of lines) {
5561 const t = line.trim().toUpperCase();
5662 if (t === "BEGIN:VCARD") {
63 if (cur !== undefined) keepBroken();
5764 cur = { fn: "", n: "", org: "", emails: [], phones: [], urls: [], note: "" };
5865 continue;
5966 }
@@ -88,6 +95,7 @@ export function parseVcf(source: string): Contacts {
8895 else if (p.name === "URL") add(cur.urls, p.value);
8996 else if (p.name === "NOTE") cur.note = clip(unescapeText(p.value).trim());
9097 }
91 // A card that never closes is dropped, not guessed at.
98 // A card that never closes is shown as a row saying so, not guessed at.
99 if (cur !== undefined) keepBroken();
92100 return { cards, total };
93101 }
src/web/render/vcf.ts +1
@@ -14,6 +14,7 @@ const fact = (label: string, values: readonly string[]): Html | null =>
1414 values.length === 0 ? null : html`<div><dt>${label}</dt><dd>${values.join(", ")}</dd></div>`;
1515
1616 function row(c: Card): Html {
17 if (c.malformed) return html`<div class="status-row"><dt><span class="muted">${c.name}</span></dt><dd></dd></div>`;

Non-blocking: the empty <dd></dd> gives screen readers an empty definition. Drop it, or put a plain-words hint there such as "Its details are left out".

1718 const facts = [fact("Email", c.emails), fact("Phone", c.phones), fact("Web", c.urls), fact("Note", c.note === "" ? [] : [c.note])].filter(
1819 (f): f is Html => f !== null,
1920 );
test/render-vcf.test.ts +32 −1
@@ -62,6 +62,37 @@ describe("contacts files", () => {
6262
6363 test("a file that is not contacts falls back to source", () => {
6464 expect(draw("hello").fallback).toContain("BEGIN:VCARD");
65 expect(draw("BEGIN:VCARD\nFN:Unclosed\n").fallback).toContain("no complete cards");
65 });

Box 4: add a test of a malformed card combined with the 500-card bound (counted in total, not pushed past the cap).

66
67 test("a card that never closes is a row saying so, not an error", () => {
68 const out = draw(card("FN:Ada") + "BEGIN:VCARD\nFN:Unclosed\n");
69 expect(out.fallback).toBeUndefined();
70 expect(html(out)).toContain("Ada");
71 expect(html(out)).toContain("Malformed card: no END:VCARD");
72 expect(html(out)).not.toContain("Unclosed");
73 expect(html(out)).toContain('<span class="muted">Malformed card: no END:VCARD</span>');
74 expect(out.summary).toContain("2 contacts");
75 const mid = draw("BEGIN:VCARD\nFN:A\nBEGIN:VCARD\nFN:B\nEND:VCARD\n");
76 expect(html(mid)).toContain("Malformed card");
77 expect(html(mid)).toContain("B");
78 });
79
80 test("vCard 4.0 reads like 3.0: tel: uris, grouped properties, lowercase names", () => {
81 const r = parseVcf("BEGIN:VCARD\r\nVERSION:4.0\r\nfn:Grace Hopper\r\nitem1.EMAIL;PREF=1:[email protected]\r\nTEL;VALUE=uri:tel:+1-555-0101\r\nORG:US Navy\r\nEND:VCARD\r\n");
82 if ("error" in r) throw new Error(r.error);
83 expect(r.cards[0]).toEqual({ name: "Grace Hopper", org: "US Navy", emails: ["[email protected]"], phones: ["+1-555-0101"], urls: [], note: "" });
84 });
85
86 test("a malformed card past the cap is counted but not pushed", () => {
87 const big = Array.from({ length: CARD_CAP }, (_, i) => card(`FN:P${i}`)).join("") + "BEGIN:VCARD\nFN:Late\n";
88 const r = parseVcf(big);
89 if ("error" in r) throw new Error(r.error);
90 expect(r.total).toBe(CARD_CAP + 1);
91 expect(r.cards.length).toBe(CARD_CAP);
92 expect(r.cards.some((c) => c.malformed)).toBe(false);
93 });
94
95 test("the size bound is 1 MB", () => {
96 expect(RENDER_CAP.contacts).toBe(1_000_000);
6697 });
6798 });
test/tui-workspace-formats.test.ts +20
@@ -234,6 +234,26 @@ describe("the terminal workspace, by file type", () => {
234234 }
235235 });
236236
237 test("a .vcf card with no END:VCARD shows as a parenthesised malformed row", async () => {
238 const made = await createRepo(forge, alice, { name: "broken", visibility: "public", description: "contacts" });
239 if (!made.ok) throw new Error("repo failed");
240 await commitToBranch(forge, alice, made.value, {
241 message: "seed",
242 files: [{ path: "bad.vcf", content: "BEGIN:VCARD\r\nFN:Ada\r\nEND:VCARD\r\nBEGIN:VCARD\r\nFN:Cut\r\n" }],
243 });
244 const h = boot();
245 try {
246 await h.settle();
247 await open(h, "broken");
248 await openFile(h, "bad.vcf");
249 expect(h.text()).toContain("(Malformed card: no END:VCARD)");
250 expect(h.text()).toContain("2 contacts");
251 expect(h.text()).not.toContain("Cut");
252 } finally {
253 h.stop();
254 }
255 });
256
237257 test("an .eml opens as its header rows and body, and s flips to the source", async () => {
238258 const made = await createRepo(forge, alice, { name: "mail", visibility: "public", description: "mail" });
239259 if (!made.ok) throw new Error("repo failed");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs