web: render stage-turn markdown in the artifact viewer

The full-screen viewer now opens markdown/text artifacts (text/markdown,
text/plain, .md/.markdown/.txt) through the resolved artifact URL and renders
them with the page's own DOM markdown renderer, with an honest failure message
and an external fallback. ArrowLeft/ArrowRight page the shared media+document
list, and a backdrop click closes the viewer while panel clicks do not.

The run gallery and per-visit artifacts present input.md, memory.md and
output.md as ordinary Document tiles with a drawn glyph and open the viewer.

Adds isMarkdownArtifact/isViewerArtifact with unit coverage, deterministic
viewer fixtures, and ui-smoke assertions/captures for rendered markdown,
keyboard paging, the honest 404 and backdrop dismissal.
This commit is contained in:
2026-10-19 21:23:10 +01:00
parent b2fd614ba4
commit 359912f822
11 changed files with 695 additions and 48 deletions
+40 -11
View File
@@ -94,6 +94,9 @@ const DOCUMENT_EXTENSIONS = new Set([
"txt", "md", "markdown", "rst", "doc", "docx", "odt", "rtf", "html", "htm",
]);
/** The text file kinds the browser can render in the artifact viewer. */
const MARKDOWN_EXTENSIONS = new Set(["md", "markdown", "txt"]);
const DOCUMENT_MIMES = new Set([
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@@ -195,6 +198,31 @@ export function artifactKindLabel(kind) {
return KIND_LABELS[kind] ?? "File";
}
/**
* Whether an artifact is text the reader can render as markdown in the viewer:
* `text/markdown` or `text/plain` (parameters ignored), or a `.md`, `.markdown`
* or `.txt` name. The viewer uses the page's existing markdown renderer, so these
* are the text files it is allowed to fetch and draw. Pure and total, mirroring
* the content-type tests above.
*/
export function isMarkdownArtifact(contentType, name) {
const mime = String(contentType ?? "").split(";")[0].trim().toLowerCase();
if (mime === "text/markdown" || mime === "text/plain") return true;
return MARKDOWN_EXTENSIONS.has(extensionOf(name));
}
/**
* Whether an artifact opens inside the full-screen viewer: the two media kinds
* the gallery already previewed, plus the markdown/text documents it now renders
* itself. The kind gate keeps a `text/plain` `.log`, `.py` or `.csv` an external
* file: those are evidence, not prose. This is the one predicate the grid tiles,
* paging and the viewer share, so they cannot disagree about the list.
*/
export function isViewerArtifact(contentType, name) {
if (isImageContentType(contentType) || isVideoContentType(contentType)) return true;
return artifactKind(contentType, name) === ArtifactKind.Document && isMarkdownArtifact(contentType, name);
}
/**
* Human-readable byte size, matching Android Format.kt humanBytes (and the TUI):
* whole bytes under 1 KiB, otherwise one decimal with a binary unit
@@ -320,28 +348,29 @@ export function artifactCaptureNotes(step) {
}
/**
* The media artifacts `groups` hold, in the exact order the gallery draws them
* (group by group, each group's images and videos in recorded order): the
* neighbours the full-screen viewer pages through, so a swipe visits the same
* list the grid showed. Non-media artifacts open externally and are not part of
* it. Mirrors ArtifactViewer.kt pagedArtifacts.
* The artifacts `groups` hold that the full-screen viewer can page through, in
* the exact order the gallery draws them (group by group, each group's images,
* videos and markdown/text documents in recorded order): the neighbours a
* swipe/keyboard step visits, so the viewer walks the same list the grid showed.
* Other artifacts open externally and are not part of it. Mirrors
* ArtifactViewer.kt pagedArtifacts, extended for markdown-first stage turns.
*/
export function pagedArtifacts(groups) {
const viewable = [];
for (const group of groups ?? []) {
for (const artifact of group?.artifacts ?? []) {
const kind = artifactKind(artifact?.content_type, artifact?.name);
if (kind === ArtifactKind.Image || kind === ArtifactKind.Video) viewable.push(artifact);
if (isViewerArtifact(artifact?.content_type, artifact?.name)) viewable.push(artifact);
}
}
return viewable;
}
/**
* Each gallery media artifact's url mapped to its position in `pagedArtifacts`,
* the same ordered list the viewer pages through. The grid's tiles resolve their
* viewer index here, so tile and viewer derive from one source and cannot drift.
* The first occurrence of a url wins. Mirrors ArtifactViewer.kt viewerIndexByUrl.
* Each viewer-selectable artifact's url mapped to its position in
* `pagedArtifacts`, the same ordered list the viewer pages through. The grid's
* tiles resolve their viewer index here, so tile and viewer derive from one
* source and cannot drift. The first occurrence of a url wins. Mirrors
* ArtifactViewer.kt viewerIndexByUrl.
*/
export function viewerIndexByUrl(groups) {
const indices = new Map();