+ {#each blocks as block, i (i)}
+ {#if block.kind === 'heading' && block.level === 2}
+ {@render inlines(block.inlines)}
+ {:else if block.kind === 'heading'}
+ {@render inlines(block.inlines)}
+ {:else if block.kind === 'paragraph'}
+ {@render inlines(block.inlines)}
+ {:else}
+
+ {#each block.items as item, j (j)}
+ - {@render inlines(item)}
+ {/each}
+
+ {/if}
+ {/each}
+
+
+
diff --git a/packages/ui/src/letter/parse.ts b/packages/ui/src/letter/parse.ts
new file mode 100644
index 0000000..c85148f
--- /dev/null
+++ b/packages/ui/src/letter/parse.ts
@@ -0,0 +1,85 @@
+/**
+ * The Letter from the Gardener, from the author's Markdown to blocks the page
+ * renders (.agents/features/letter-slot.md L3, L4).
+ *
+ * A deliberately small subset: headings, paragraphs, lists, *emphasis* and
+ * links. Anything else is shown as the text it is. Nothing here builds HTML,
+ * so nothing in the file can inject markup or script.
+ */
+
+export type Inline =
+ | { readonly kind: 'text'; readonly text: string }
+ | { readonly kind: 'em'; readonly text: string }
+ | { readonly kind: 'link'; readonly text: string; readonly href: string };
+
+export type LetterBlock =
+ | { readonly kind: 'heading'; readonly level: 2 | 3; readonly inlines: readonly Inline[] }
+ | { readonly kind: 'paragraph'; readonly inlines: readonly Inline[] }
+ | { readonly kind: 'list'; readonly items: readonly (readonly Inline[])[] };
+
+/** The support door needs web and phone links, and nothing else does. */
+const ALLOWED_HREF = /^(?:https:\/\/\S+|tel:\+?[\d ()-]+)$/i;
+
+// A link target may hold one level of brackets, as in tel:+420 (2) 123.
+const INLINE = /\[([^\]\n]+)\]\(((?:[^()\n]|\([^()\n]*\))+)\)|\*([^*\n]+)\*|_([^_\n]+)_/g;
+
+function inlines(source: string): Inline[] {
+ const parts: Inline[] = [];
+ const text = (t: string): void => {
+ if (t === '') return;
+ const last = parts[parts.length - 1];
+ if (last?.kind === 'text') parts[parts.length - 1] = { kind: 'text', text: last.text + t };
+ else parts.push({ kind: 'text', text: t });
+ };
+
+ let at = 0;
+ for (const m of source.matchAll(INLINE)) {
+ text(source.slice(at, m.index));
+ const [, label, href, star, underscore] = m;
+ if (label !== undefined && href !== undefined) {
+ if (ALLOWED_HREF.test(href.trim())) parts.push({ kind: 'link', text: label, href: href.trim() });
+ else text(label);
+ } else {
+ parts.push({ kind: 'em', text: star ?? underscore ?? '' });
+ }
+ at = m.index + m[0].length;
+ }
+ text(source.slice(at));
+ return parts;
+}
+
+export function parseLetter(markdown: string): LetterBlock[] {
+ // Comments are the author's notes and the scaffold's slots, never the letter.
+ const lines = markdown.replace(/\r\n?/g, '\n').replace(//g, '').split('\n');
+ const blocks: LetterBlock[] = [];
+ let paragraph: string[] = [];
+ let list: Inline[][] = [];
+
+ const flush = (): void => {
+ if (paragraph.length > 0) blocks.push({ kind: 'paragraph', inlines: inlines(paragraph.join(' ')) });
+ if (list.length > 0) blocks.push({ kind: 'list', items: list });
+ paragraph = [];
+ list = [];
+ };
+
+ for (const raw of lines) {
+ const line = raw.trim();
+ const heading = /^(#{1,6})\s+(.+)$/.exec(line);
+ const item = /^[-*]\s+(.+)$/.exec(line);
+
+ if (line === '') {
+ flush();
+ } else if (heading !== null) {
+ flush();
+ blocks.push({ kind: 'heading', level: heading[1] === '#' ? 2 : 3, inlines: inlines(heading[2] ?? '') });
+ } else if (item !== null) {
+ if (paragraph.length > 0) flush();
+ list.push(inlines(item[1] ?? ''));
+ } else {
+ if (list.length > 0) flush();
+ paragraph.push(line);
+ }
+ }
+ flush();
+ return blocks;
+}
diff --git a/packages/ui/test/Shell.test.ts b/packages/ui/test/Shell.test.ts
index 4c3846d..d244fa6 100644
--- a/packages/ui/test/Shell.test.ts
+++ b/packages/ui/test/Shell.test.ts
@@ -1,10 +1,23 @@
import { PRODUCT_NAME } from '@rosea/schema';
-import { cleanup, render, screen } from '@testing-library/svelte';
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/svelte';
import { afterEach, describe, expect, it } from 'vitest';
import Shell from '../src/Shell.svelte';
import { en } from '../src/copy/en.js';
-afterEach(cleanup);
+afterEach(() => {
+ cleanup();
+ window.location.hash = '';
+});
+
+const LETTER = [
+ '# Why',
+ '',
+ 'A line.',
+ '',
+ '[Read](https://example.org) or [call](tel:+420123) or [not](javascript:alert(1)).'
+].join('\n');
+
+const h1 = (): HTMLElement => screen.getByRole('heading', { level: 1 });
describe('Shell', () => {
it('names the product in the page heading, from the schema constant', () => {
@@ -24,3 +37,49 @@ describe('Shell', () => {
expect(screen.getByRole('main')).toBeTruthy();
});
});
+
+describe('Shell and the Letter', () => {
+ it('with an empty letter offers no way to it, and #letter shows the garden', () => {
+ window.location.hash = '#letter';
+ render(Shell, { letter: '\n' });
+ expect(h1().textContent).toBe(PRODUCT_NAME);
+ expect(screen.queryAllByRole('link')).toEqual([]);
+ });
+
+ it('with a letter, a link opens it, Back returns, and focus follows each heading', async () => {
+ render(Shell, { letter: LETTER });
+ expect(document.activeElement, 'nothing is focused on first load').toBe(document.body);
+
+ await fireEvent.click(screen.getByRole('link', { name: en.letter.title }));
+ await waitFor(() => expect(h1().textContent).toBe(en.letter.title));
+ await waitFor(() => expect(document.activeElement).toBe(h1()));
+ expect(screen.getByRole('heading', { level: 2 }).textContent).toBe('Why');
+ expect(screen.getByRole('main')).toBeTruthy();
+
+ await fireEvent.click(screen.getByRole('link', { name: en.letter.back }));
+ await waitFor(() => expect(h1().textContent).toBe(PRODUCT_NAME));
+ await waitFor(() => expect(document.activeElement).toBe(h1()));
+ });
+
+ it('opens straight at #letter, as from a bookmark', () => {
+ window.location.hash = '#letter';
+ render(Shell, { letter: LETTER });
+ expect(h1().textContent).toBe(en.letter.title);
+ });
+
+ it('renders https links in a new tab with no referrer, tel links in place, and nothing else as a link', () => {
+ window.location.hash = '#letter';
+ render(Shell, { letter: LETTER });
+ const read = screen.getByRole('link', { name: 'Read' });
+ expect(read.getAttribute('href')).toBe('https://example.org');
+ expect(read.getAttribute('target')).toBe('_blank');
+ expect(read.getAttribute('rel')).toBe('noopener noreferrer');
+
+ const call = screen.getByRole('link', { name: 'call' });
+ expect(call.getAttribute('href')).toBe('tel:+420123');
+ expect(call.getAttribute('target')).toBeNull();
+
+ expect(screen.queryByRole('link', { name: 'not' })).toBeNull();
+ expect(screen.getByText(/or not\./)).toBeTruthy();
+ });
+});
diff --git a/packages/ui/test/letter-parse.test.ts b/packages/ui/test/letter-parse.test.ts
new file mode 100644
index 0000000..ef46b79
--- /dev/null
+++ b/packages/ui/test/letter-parse.test.ts
@@ -0,0 +1,78 @@
+import { describe, expect, it } from 'vitest';
+import { parseLetter } from '../src/letter/parse.js';
+
+describe('parseLetter', () => {
+ it('reads a file of only comments as an empty letter', () => {
+ const scaffold = '\n\n\n\n';
+ expect(parseLetter(scaffold)).toEqual([]);
+ expect(parseLetter('')).toEqual([]);
+ expect(parseLetter(' \n\n \r\n')).toEqual([]);
+ });
+
+ it('reads headings, paragraphs joined across lines, and lists', () => {
+ const md = '# Why\n\nOne line\nand the next.\n\n## How\n- first\n* second\n\nLast.';
+ expect(parseLetter(md)).toEqual([
+ { kind: 'heading', level: 2, inlines: [{ kind: 'text', text: 'Why' }] },
+ { kind: 'paragraph', inlines: [{ kind: 'text', text: 'One line and the next.' }] },
+ { kind: 'heading', level: 3, inlines: [{ kind: 'text', text: 'How' }] },
+ { kind: 'list', items: [[{ kind: 'text', text: 'first' }], [{ kind: 'text', text: 'second' }]] },
+ { kind: 'paragraph', inlines: [{ kind: 'text', text: 'Last.' }] }
+ ]);
+ });
+
+ it('maps every heading below the first level to h3', () => {
+ expect(parseLetter('### Deep\n\n###### Deeper')).toEqual([
+ { kind: 'heading', level: 3, inlines: [{ kind: 'text', text: 'Deep' }] },
+ { kind: 'heading', level: 3, inlines: [{ kind: 'text', text: 'Deeper' }] }
+ ]);
+ });
+
+ it('reads CRLF files the same as LF', () => {
+ expect(parseLetter('# A\r\n\r\nB\r\nC')).toEqual(parseLetter('# A\n\nB\nC'));
+ });
+
+ it('reads emphasis and links inside a paragraph', () => {
+ expect(parseLetter('A *quiet* door: [call](tel:+420 123 456) or [read](https://example.org/x).')).toEqual([
+ {
+ kind: 'paragraph',
+ inlines: [
+ { kind: 'text', text: 'A ' },
+ { kind: 'em', text: 'quiet' },
+ { kind: 'text', text: ' door: ' },
+ { kind: 'link', text: 'call', href: 'tel:+420 123 456' },
+ { kind: 'text', text: ' or ' },
+ { kind: 'link', text: 'read', href: 'https://example.org/x' },
+ { kind: 'text', text: '.' }
+ ]
+ }
+ ]);
+ });
+
+ it('reads a phone number with brackets as one link', () => {
+ expect(parseLetter('[call](tel:+420 (2) 123)')).toEqual([
+ { kind: 'paragraph', inlines: [{ kind: 'link', text: 'call', href: 'tel:+420 (2) 123' }] }
+ ]);
+ });
+
+ it('keeps only the label of a link whose scheme is not https: or tel:', () => {
+ for (const href of ['javascript:alert(1)', 'data:text/html,x', 'http://example.org', '/relative', 'JavaScript:x']) {
+ expect(parseLetter(`[label](${href})`), href).toEqual([
+ { kind: 'paragraph', inlines: [{ kind: 'text', text: 'label' }] }
+ ]);
+ }
+ });
+
+ it('never produces markup: raw HTML stays text', () => {
+ const blocks = parseLetter('\n\n