Repository navigation
Draw a Markdown file's preview as GitHub does: its HTML, and the pictures and files beside it - #7310
Merged
Merged
Conversation
…ures and files beside it The maintainer picked rendering the HTML GitHub allows in a file's preview, and on loading the pictures a file names beside it wrote: "why limit to these files? why not just allow anything the server can reach? I'm not sure we're really designed to have a proper sandbox at this point, we shouldn't add complication unless we're actually tight here". Of the 197 most-starred Markdown READMEs, 81% hold raw HTML, which the preview printed as tags, 46% name pictures by a path beside the file, which it couldn't load, and 80% link to another file by a path, which took the page itself to a missing address. - markdown.js gains `markdownFileFragment`, a file's reading with HTML, kept to GitHub's allowlist by DOMPurify 3.5.0 (31 KB, 12 KB compressed), which loads for the first file holding HTML. Beyond GitHub's list it drops `accesskey` and `tabindex`, which would rebind the page's keys and tab order, `data-` attributes, which Leaf's own marks use, and every class but a code block's language and a task list's, `rel`, which could keep an opener for a page a link opens, and the attributes that name another element by id, as `for` and `aria-labelledby` do, which could name one of the page's; an `id` or `name` takes GitHub's `user-content-` prefix, a style stands only as a table cell's alignment, which the reading writes so, and an `<input>` only as a disabled checkbox. The host's links and pictures apply after the sanitizer, to the file's HTML links as to its Markdown ones. Drafts and messages keep showing tags as text, since Python draws a draft's body too and has no sanitizer. - A bound file's read gives `url`, the address of its bytes under `/api/host-files/<absolute path>`, where the server answers them. A live page's server answers any file there it can read, but only to a request carrying the page's key, so a sample's capability and the website's container answer none and give no `url`, and never from Leaf's own state home, which holds each page's connection credentials that a page's scripts never receive. Text answers as text/plain, so a linked file shows rather than downloads, and every answer is sandboxed by `Content-Security-Policy: sandbox`, so an SVG a README names, opened on its own, runs no script as the page. The file editor passes the url in its text-document output, whose contract gains it, and the preview resolves a picture's `src` and `srcset` and a link to another file against it, `../` included, opening a linked file in a new tab. This puts a bound file's host path in the browser, which the file editor had kept out since #2202; it shows on no screen. - A link to an anchor the file names, as `<a name="top">`, lands on it as a link to a heading does: the preview carries the file's ids and names as places of its own (`data-lf-place`) rather than page ids. A fold the reader opened stays open as they type inside it: a redrawn fold takes the state of the drawn one with its summary. - The preview's pictures stand bare, without the frame the reading face draws round a picture, as on GitHub. - An image sized by its `height` alone keeps that height on every page, drawn whole within it where its container is narrower: base.css forced `height: auto` on every image, which drew 167 logos in 35 of the READMEs at their files' size. No page in the corpus has such an image. On sparkjoy's README all 20 pictures load and no tag shows as text, in Chromium and Firefox. Tests: the preview's browser test plants a fold it then types inside, a centred picture beside the file, a link to another file and to a named anchor, an aligned table, a script, a handler, a `javascript:` link, an opener, a styled div with Leaf's class, data attribute and a page id, a text field and a task list; a runtime test holds a host's moved picture in place; a model test reads a picture above the bound file's folder through its url and checks the sandbox and the key. A written passage's test escapes its `<missing>` placeholder, which GitHub's reading drops as an unknown tag. Reviewed in three Codex passes.
…ting # Conflicts: # packages/default/widgets/lf-text-document.js
leaf-agent
previously approved these changes
Nov 26, 2026
…a filename's no-break space in srcset
leaf-agent
previously approved these changes
Nov 26, 2026
…ting # Conflicts: # browser/types/contract.d.ts # packages/default/registry.json # pnpm-lock.yaml # tests/browser/test_render_files.py # tests/model/test_file_bindings.py
… to the file loads its pictures and the browser holds no host path
…rver, and test the state home on a file that exists
leaf-agent
approved these changes
Nov 26, 2026
max-sixty
added a commit
that referenced
this pull request
Nov 26, 2026
…itor (#7315) A double-click in a Markdown file's preview now lands on the right line when the file holds HTML. Since #7310 the preview draws a file's HTML as GitHub does, but the double-click from #7297 placed the file's tokens with `html: false`, so an HTML block's words were its tags, which never appear in the preview, and a double-click inside a centred `<p align="center">` header or an HTML fold landed on the next Markdown block's line. Placement now parses a file the way the drawing did: `markdownSourceOffset` takes the rendering's `html` flag, and each caller names it (the preview `true`, `lf-draft` `false`), which the type check enforces. An HTML block is placed whole and aligned by the words its source draws: the text outside markup (tags, comments, declarations, script and style), with character references decoded and `<br>` as a line break. So a word never lands in an attribute or comment that spells it, text the sanitizer drops takes no part, and a large HTML table maps in milliseconds. Text the browser's parser moves out of source order, such as foster-parented table text or a `<pre>`'s first newline, still sends a click in that block to the next block, and a half-glyph click beside markup that spans lines inside a blockquote, or beside a multiline comment, can take the neighbouring line. The mapping pairs words in document order rather than elements with tokens, so Markdown that a fold wraps maps as before. On 200 popular READMEs drawn through `markdownFileFragment` in Chromium, all 2,246 sampled words now map to the word's exact source position; before, 1,779 landed on a line holding the word. A lookup's p99 is 13 ms; a 160-row HTML table takes 0.7 ms, where aligning against its tags took 255 ms. Follows the parent session's request to fix the double-click for #7310's previews; reviewed in four Codex passes. Tests: the browser double-click test's README gains a centred header with a `title`, comment and `<style>` spelling its word, and a fold with Markdown inside; it fails without the change. A runtime case covers the same mapping, with quoted `>`, character references with and without semicolons, and both sides of markup that spans lines. > _This was written by Claude Code on behalf of max-sixty_
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The maintainer picked rendering the HTML GitHub allows in a file's preview, and on loading the pictures a file names beside it wrote: "why limit to these files? why not just allow anything the server can reach? I'm not sure we're really designed to have a proper sandbox at this point, we shouldn't add complication unless we're actually tight here". Of the 197 most-starred Markdown READMEs, 81% hold raw HTML, which the preview printed as tags, 46% name pictures by a path beside the file, which it couldn't load, and 80% link to another file by a path, which took the page itself to a missing address.
markdownFileFragment, a file's reading with HTML, kept to GitHub's allowlist by DOMPurify 3.5.0 (31 KB, 12 KB compressed), which loads for the first file holding HTML. Beyond GitHub's list it dropsaccesskeyandtabindex, which would rebind the page's keys and tab order,data-attributes, which Leaf's own marks use, and every class but a code block's language and a task list's,rel, which could keep an opener for a page a link opens, and the attributes that name another element by id, asforandaria-labelledbydo, which could name one of the page's; anidornametakes GitHub'suser-content-prefix, a style stands only as a table cell's alignment, which the reading writes so, and an<input>only as a disabled checkbox. The host's links and pictures apply after the sanitizer, to the file's HTML links as to its Markdown ones. Drafts and messages keep showing tags as text, since Python draws a draft's body too and has no sanitizer.boundFile(binding).beside(path)gives the address/api/files/<binding>/beside?path=<path>, where a live page's server answers the file at that path from the bound file's folder,../included, or null where the page can't reach its server's files, as in an export. It answers any file it can read, but only to a request carrying the page's key, so a sample's capability and the website's container answer none, and never from Leaf's own state home, which holds each page's connection credentials that a page's scripts never receive. Text answers as text/plain, so a linked file shows rather than downloads, and every answer is sandboxed byContent-Security-Policy: sandbox, so an SVG a README names, opened on its own, runs no script as the page. A preview of a file the page binds, named by its value'skeyas a bound file's source (feat: make each bound file a data source under its binding's id #7306) and its editor both name it, resolves a picture'ssrcandsrcsetand a link to another file through it, opening a linked file in a new tab. Asking by the binding keeps the file's host path out of the browser, as the file editor always has; an earlier version handed the browser an address spelling the path.<a name="top">, lands on it as a link to a heading does: the preview carries the file's ids and names as places of its own (data-lf-place) rather than page ids. A fold the reader opened stays open as they type inside it: a redrawn fold takes the state of the drawn one with its summary, within the same file. A table cell an HTML table aligns withalign, as 27 of the READMEs do, keeps that alignment, which the reading face'stext-align: startoverrode on every page.heightalone keeps that height on every page, drawn whole within it where its container is narrower: base.css forcedheight: autoon every image, which drew 167 logos in 35 of the READMEs at their files' size. No page in the corpus has such an image.On sparkjoy's README all 20 pictures load and no tag shows as text, in Chromium and Firefox.
Left as they are, each rare: a double-click on a file's HTML text can put the editor's caret on the wrong line, since #7297's source mapping parses without HTML, which the editor session that built it is changing next; two previews whose files group folds under one
<details name>share that group; an<a>with bothidandnameanswers only its id; a.svgzpicture doesn't draw; a live preview whose value'skeynames no binding asks the server for its pictures and gets none; after a rebind to another file, a picture at the same path can show from the browser's cache until a reload; a reference keeps no#fragment, and one whose..passes through a missing folder, or whose name mixes a bare%with escapes, finds no file.Tests: the preview's browser test plants a fold it then types inside, a centred picture beside the file, a link to another file and to a named anchor, an aligned table, a script, a handler, a
javascript:link, an opener, a styled div with Leaf's class, data attribute and a page id, a text field and a task list; a runtime test holds a host's moved picture in place; a model test reads a picture above the bound file's folder beside its binding and checks the sandbox, the key and Leaf's state home. A written passage's test escapes its<missing>placeholder, which GitHub's reading drops as an unknown tag. Reviewed in five Codex passes.