Public: anyone can read this; the crew can change it.
A .vcf contacts file opens as rows: name, phone, email, organisation #13091
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13073-a-vcf-contacts-file-opens-as-rows-name-p → main 5 commits fork 3 threads · 3 unresolved
7 files
changed +65
−5
docs/features/vcf-view.md +1 −1
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | 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. |
| 4 | 4 | |
| 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. | |
| 6 | 6 | - **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`. |
| 7 | 7 | |
| 8 | 8 | 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 { | ||
| 394 | 394 | const shown = parsed.cards.slice(0, CARD_CAP); |
| 395 | 395 | return { |
| 396 | 396 | 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(", ")]), | |
| 398 | 398 | more: parsed.total - shown.length, |
| 399 | 399 | delimiter: ",", |
| 400 | 400 | columns: 4, |
src/web/render/kinds.ts +1 −1
| @@ -107,7 +107,7 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = { | ||
| 107 | 107 | otio: 1 * MB, |
| 108 | 108 | edl: 1 * MB, |
| 109 | 109 | fcpxml: 1 * MB, |
| 110 | contacts: 5 * MB, | |
| 110 | contacts: 1 * MB, | |
| 111 | 111 | mail: 5 * MB, |
| 112 | 112 | gcode: 50 * MB, |
| 113 | 113 | dxf: 5 * MB, |
src/web/render/vcf-parse.ts +9 −1
| @@ -22,6 +22,8 @@ export interface Card { | ||
| 22 | 22 | readonly phones: string[]; |
| 23 | 23 | readonly urls: string[]; |
| 24 | 24 | readonly note: string; |
| 25 | /** The card never closed with END:VCARD; its fields are not trusted. */ | |
| 26 | readonly malformed?: true; | |
| 25 | 27 | } |
| 26 | 28 | |
| 27 | 29 | export type Contacts = { readonly cards: Card[]; readonly total: number } | { readonly error: string }; |
| @@ -51,9 +53,14 @@ export function parseVcf(source: string): Contacts { | ||
| 51 | 53 | const cards: Card[] = []; |
| 52 | 54 | let total = 0; |
| 53 | 55 | 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 | }; | |
| 54 | 60 | for (const line of lines) { |
| 55 | 61 | const t = line.trim().toUpperCase(); |
| 56 | 62 | if (t === "BEGIN:VCARD") { |
| 63 | if (cur !== undefined) keepBroken(); | |
| 57 | 64 | cur = { fn: "", n: "", org: "", emails: [], phones: [], urls: [], note: "" }; |
| 58 | 65 | continue; |
| 59 | 66 | } |
| @@ -88,6 +95,7 @@ export function parseVcf(source: string): Contacts { | ||
| 88 | 95 | else if (p.name === "URL") add(cur.urls, p.value); |
| 89 | 96 | else if (p.name === "NOTE") cur.note = clip(unescapeText(p.value).trim()); |
| 90 | 97 | } |
| 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(); | |
| 92 | 100 | return { cards, total }; |
| 93 | 101 | } |
src/web/render/vcf.ts +1
| @@ -14,6 +14,7 @@ const fact = (label: string, values: readonly string[]): Html | null => | ||
| 14 | 14 | values.length === 0 ? null : html`<div><dt>${label}</dt><dd>${values.join(", ")}</dd></div>`; |
| 15 | 15 | |
| 16 | 16 | 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". |
||
| 17 | 18 | const facts = [fact("Email", c.emails), fact("Phone", c.phones), fact("Web", c.urls), fact("Note", c.note === "" ? [] : [c.note])].filter( |
| 18 | 19 | (f): f is Html => f !== null, |
| 19 | 20 | ); |
test/render-vcf.test.ts +32 −1
| @@ -62,6 +62,37 @@ describe("contacts files", () => { | ||
| 62 | 62 | |
| 63 | 63 | test("a file that is not contacts falls back to source", () => { |
| 64 | 64 | 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); | |
| 66 | 97 | }); |
| 67 | 98 | }); |
test/tui-workspace-formats.test.ts +20
| @@ -234,6 +234,26 @@ describe("the terminal workspace, by file type", () => { | ||
| 234 | 234 | } |
| 235 | 235 | }); |
| 236 | 236 | |
| 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 | ||
| 237 | 257 | test("an .eml opens as its header rows and body, and s flips to the source", async () => { |
| 238 | 258 | const made = await createRepo(forge, alice, { name: "mail", visibility: "public", description: "mail" }); |
| 239 | 259 | if (!made.ok) throw new Error("repo failed"); |
new above
offline
Nothing reads
malformed. Use it in vcf.tsrow()to render the row as muted text, not as a contact name, and in the TUI table.