Code blocks
Code in a document sits in a frame that copies it, says what it is, and keeps a long block from burying the text around it.
Give every block of code in a document the same frame: Copy, its language or the file it is, and a cut on a long one. The frame carries the controls, so at rest the code is never covered and it never moves.
Start the stack with the compose file:
services:
web:
image: nginx:1.27
ports:
- "8080:80"Then run the tests:
pnpm install && pnpm testThe whole pipeline, for reference:
steps:
- run: pnpm --filter step-1 test
- run: pnpm --filter step-2 test
- run: pnpm --filter step-3 test
- run: pnpm --filter step-4 test
- run: pnpm --filter step-5 test
- run: pnpm --filter step-6 test
- run: pnpm --filter step-7 test
- run: pnpm --filter step-8 test
- run: pnpm --filter step-9 test
- run: pnpm --filter step-10 test
- run: pnpm --filter step-11 test
- run: pnpm --filter step-12 test
- run: pnpm --filter step-13 test
- run: pnpm --filter step-14 test
- run: pnpm --filter step-15 test
- run: pnpm --filter step-16 test
- run: pnpm --filter step-17 test
- run: pnpm --filter step-18 test
- run: pnpm --filter step-19 test
- run: pnpm --filter step-20 test
- run: pnpm --filter step-21 test
- run: pnpm --filter step-22 test
- run: pnpm --filter step-23 test
- run: pnpm --filter step-24 test
- run: pnpm --filter step-25 test
- run: pnpm --filter step-26 test
- run: pnpm --filter step-27 test
- run: pnpm --filter step-28 test
- run: pnpm --filter step-29 test
- run: pnpm --filter step-30 test
- run: pnpm --filter step-31 test
- run: pnpm --filter step-32 test
- run: pnpm --filter step-33 test
- run: pnpm --filter step-34 test
- run: pnpm --filter step-35 test
- run: pnpm --filter step-36 testThree fences from a setup note. The first names its file, so its header says compose.yaml and
offers Download, which saves the block under that name. The second has only a language, which
shows with Copy when the pointer is over the block. The third is longer than
30 lines, so it is cut, and Show all under it lets the whole of it into
the page.
Naming a file#
Write the filename after the language, ```yaml compose.yaml, or as
```ts title="retry.ts". Other renderers read the first word and ignore the rest, so the
fence still highlights wherever it is pasted. Name a file when the reader will save the block as
one: a config to drop in, a script to run. Its header then offers Download; the name itself
only names, and never downloads. A snippet that is only read keeps its language alone.
Long blocks#
A block past 30 lines is cut, so the prose after it is not pushed a screen away, and a patch is cut once it is long. They are cut as every long block in a document is; see Long content for where Show all goes and how to turn it off.
Long lines#
A fence, a snippet and a diff wrap a line too long for the column rather than scroll it sideways, so a phone shows all of it. Each row a line wraps onto starts a little past the line's own indentation, behind a faint ↳, so the reader can tell a statement that goes on from the next one at any depth. A commit message is prose and wraps flush. A whole file shown with its line numbers scrolls sideways instead, keeping each line on one row beside its number.
Copy#
Copy copies the block as written, whichever view shows. It turns into a check for a moment and
says "Copied" to a screen reader. It shows no toast: the eyes are already on the button. From a
header's ⋯, where no button is left to turn, it toasts, and so does a failure, since the button
cannot say why; see Feedback.
Drawings and the text they are drawn from#
A mermaid diagram and a patch sit in the same frame, and always under a header, since each holds more than Copy. Source switches either between its drawing and the text as written, held down while the text shows; its name stays the same both ways, and Copy copies the text in either. It joins once there is a drawing to switch from: not while a diagram renders or a patch parses, and never on a diagram that failed to parse. A patch of several files adds its files menu left of Source, a command of the drawn view that goes while the text shows. Under an attachment viewer, ⤢ opens either there, on what the block shows. Inside a card or the viewer, which lend their own title bar, a patch puts its controls there and draws no frame.
Every file drawn as something other than its text has the same Source, in the card's header and the viewer's title bar: rendered Markdown, a diagram, a patch, an HTML page, a mail message (its text as sent, encoded parts and all), a tree, a log. A file with one view, code, has none. A drawing that cannot be drawn leaves the text, with the reason above it, and no Source. Two ways of drawing the same view, a message's plain text or HTML, are that view's own command, left of Source and gone while the text shows; Source only ever switches between the drawing and the text. A code block in a document, a log's or a terminal capture's, has no Source: its text is the document's own.
Data as a tree#
A block of code is read as written. A payload is browsed: someone folds it open, searches it and
copies one part of it, a request dump, a config, an event's body. Show a payload as a JsonView
and a snippet as a fence or a CodeBlock. A short sample that illustrates the prose stays code,
even when it is JSON; a payload the reader came to inspect is a tree.
A JSON, YAML or TOML file follows the same line by where it is. Embedded in a document, it is read in passing, so its card opens on the text, Source held down, and a press turns it into a tree that grows to the card's cut and scrolls there. Opened in the viewer, the reader came to inspect it, so it opens as a tree.
An agent's request, written as JSON5. Its comments sit on rows of their own, or after the value
they follow, and fold away with their member. Click a key, a value or the space before a row to
select it, and Ctrl+C copies it as JSON; a range copies as one member per line. Text parsed with
parseJson keeps where each part came from, so Copy source in a row's menu copies the
selection as it was written, its indent taken off and comments included.
- Parse JSONC and JSON5 with
parseJsonand pass thedocument. A value the app already parsed goes in asvalue; it shows no comments and offers no Copy source. - Parse YAML with
parseYamlfrom@orz/ui-json/yamland pass thedocumentit resolves to; its anchors, aliases and tags show on their rows, and an alias opens onto what it names. - Parse TOML with
parseTomlfrom@orz/ui-json/tomland pass thedocumentit resolves to. Its headers build the tree a program reads, and Copy source on a table written in more than one place copies each place.parseYamlandparseTomlfetch their parser on their first call, so neither weighs on a page that never reads YAML or TOML. - Give the view a
name, the file's, and its header says it; without one the header names the format the document was read from. Its Copy copies the document as it was written, comments included, and avalueas JSON. - Give the view a
summarywhen the data has a shape a collapsed row can name. An app showing agent traces passesagentTraceSummary, so a collapsed message reads as its speaker and its text, and a tool call as the call. A view with asummarypassesdefaultExpandDepth={2}, so each record stays collapsed and reads as its summary. - Register
jsonContentbefore the code renderer: a.json,.jsoncor.json5file opens as a tree in the viewer and as text in an embed card, and one that does not parse shows as text under the error. RegisteryamlContentbeside it, from@orz/ui-json/yaml/content, for.yamland.ymlfiles, andtomlContent, from@orz/ui-json/toml/content, for.tomlfiles and the lock files written in TOML. Register them at the app's root: each fetches its tree and its parser only when a tree first shows.
On each platform#
Desktop & pointer#
- An unnamed block whose one command is Copy floats its language and Copy over its top corner, shown while the pointer is over the block or once Tab reaches Copy. At rest they take no room.
Touch#
- With no hover to bring them up, an unnamed block's language and Copy are its header instead, the row a named block has, so every block on a touch screen has one and nothing sits on its first line.
- A block's header and a cut block's bar grow to a finger's height.