[ui] LetterSlot: Add the page for A Letter from the Gardener, shown once the author writes it

docs/LETTER.md holds slots as comments only, so the player sees no change
until the author writes. The app bundles the letter at build time, a small
Markdown subset renders without {@html}, only https: and tel: become links,
and lint:copy now reads the letter. Review found turbo replaying a stale
build after the letter changed; docs/LETTER.md is now in its cache key.
This commit is contained in:
2026-11-01 15:39:17 +01:00
parent b871ab9899
commit a200832cfb
23 changed files with 639 additions and 21 deletions
+60 -4
View File
@@ -1,12 +1,55 @@
<script lang="ts">
import { PRODUCT_NAME } from '@rosea/schema';
import { en } from './copy/en.js';
import Letter from './letter/Letter.svelte';
import { parseLetter } from './letter/parse.js';
/** The author's Markdown from docs/LETTER.md. Empty means there is no way to the page. */
let { letter = '' }: { letter?: string } = $props();
const LETTER_HASH = '#letter';
// Any hash but the letter's is the garden. A bare '#' is not a valid link target.
const GARDEN_HASH = '#garden';
const blocks = $derived(parseLetter(letter));
let hash = $state(location.hash);
const view = $derived(hash === LETTER_HASH && blocks.length > 0 ? 'letter' : 'home');
let heading = $state<HTMLHeadingElement>();
// Focus follows the view only after the player moved, never on the first load.
let moved = false;
$effect(() => {
const onChange = (): void => {
moved = true;
hash = location.hash;
};
window.addEventListener('hashchange', onChange);
return () => window.removeEventListener('hashchange', onChange);
});
$effect(() => {
// Read `heading` first: an effect tracks only what it reads, and `moved` is not state.
const target = heading;
if (moved) target?.focus();
});
</script>
<main class="shell">
<h1>{PRODUCT_NAME}</h1>
<p>{en.shell.preparing}</p>
</main>
{#if view === 'letter'}
<main class="shell page">
<h1 bind:this={heading} tabindex="-1">{en.letter.title}</h1>
<Letter {blocks} />
<a href={GARDEN_HASH}>{en.letter.back}</a>
</main>
{:else}
<main class="shell">
<h1 bind:this={heading} tabindex="-1">{PRODUCT_NAME}</h1>
<p>{en.shell.preparing}</p>
{#if blocks.length > 0}
<a href={LETTER_HASH}>{en.letter.title}</a>
{/if}
</main>
{/if}
<style>
.shell {
@@ -18,6 +61,11 @@
text-align: center;
}
.page {
place-content: start center;
text-align: start;
}
h1 {
margin: 0;
font-family: var(--font-display);
@@ -27,8 +75,16 @@
color: var(--rose);
}
h1:focus {
outline: none;
}
p {
margin: 0;
color: var(--ink-soft);
}
a {
color: var(--rose);
}
</style>
+5
View File
@@ -9,6 +9,11 @@ export const en = {
shell: {
/** The one line on the Phase 0 page. True, calm, and asks nothing. */
preparing: 'The garden is being prepared.'
},
letter: {
/** The help section's name (ROADMAP §1). The letter itself is the author's, in docs/LETTER.md. */
title: 'A Letter from the Gardener',
back: 'Back to the garden'
}
} as const;
+1
View File
@@ -1,2 +1,3 @@
export { default as Shell } from './Shell.svelte';
export { en, type Copy } from './copy/en.js';
export { parseLetter, type Inline, type LetterBlock } from './letter/parse.js';
+61
View File
@@ -0,0 +1,61 @@
<script lang="ts">
import type { Inline, LetterBlock } from './parse.js';
let { blocks }: { blocks: readonly LetterBlock[] } = $props();
</script>
{#snippet inlines(parts: readonly Inline[])}
{#each parts as part, i (i)}
{#if part.kind === 'link' && part.href.startsWith('tel:')}
<a href={part.href}>{part.text}</a>
{:else if part.kind === 'link'}
<a href={part.href} target="_blank" rel="noopener noreferrer">{part.text}</a>
{:else if part.kind === 'em'}
<em>{part.text}</em>
{:else}
{part.text}
{/if}
{/each}
{/snippet}
<article class="letter">
{#each blocks as block, i (i)}
{#if block.kind === 'heading' && block.level === 2}
<h2>{@render inlines(block.inlines)}</h2>
{:else if block.kind === 'heading'}
<h3>{@render inlines(block.inlines)}</h3>
{:else if block.kind === 'paragraph'}
<p>{@render inlines(block.inlines)}</p>
{:else}
<ul>
{#each block.items as item, j (j)}
<li>{@render inlines(item)}</li>
{/each}
</ul>
{/if}
{/each}
</article>
<style>
.letter {
max-width: 38rem;
line-height: 1.65;
}
h2,
h3 {
font-family: var(--font-display);
font-weight: 400;
color: var(--rose);
margin: var(--space-4) 0 var(--space-2);
}
p,
ul {
margin: 0 0 var(--space-3);
}
a {
color: var(--rose);
}
</style>
+85
View File
@@ -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(/<!--[\s\S]*?-->/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;
}