Skip to content

Fit a written Markdown quote to its text, in both tabs - #5241

Merged
max-sixty merged 11 commits into
mainfrom
diagnose-markdown-excerpts
Nov 3, 2026
Merged

max-sixty merged 11 commits into
mainfrom
diagnose-markdown-excerpts

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

#4861 sent every quoted Markdown passage to lf-text-document, which drew it in the fixed box a bound document keeps: a one-paragraph rule sat in a 530px frame around 156px of text. The box is fixed because bound text arrives after the first paint. A written passage's text is already in the markup, so its rendering can be on the first paint, and the box can fit it.

What changes

A written Markdown preview takes its rendered height, up to --lf-bound. A new registry key, x-prepaint, names the attribute that gives the notation of an element's written <pre>; lf-text-document declares it on preview. Where it names markdown, the server (revision_delivery.mark_declared) writes the text's rendering as a Markdown file in a .lf-markdown-body beside the <pre>, as it already did for x-text-format: markdown words. The browser writes the same rendering for the documents a revision brings and for thread messages (prepaintDeclaredFiles in markdown.js, called from version.js and widget-loader.js). Until the module draws, the placeholder shows that rendering under the tab strip's room, in the drawn widget's grounds, so the first paint has the drawn height.

The Source tab keeps the preview's height. holdSource measures the preview while it shows, or, for a document drawn on Source, the box the page first painted. A source longer than that height ends in a fade under a "Show all N lines" press, an inline press like a thread's "Full quote", which shows every line in the page and moves focus to the listing. A cut source no longer scrolls inside its box; the press is the way to the rest.

Bound documents, and written ones without a Markdown preview, keep the fixed box. This also clears the keeps-first-box finding #4861 left on the default package's x-example.

Measured

In Chromium and Firefox, the first paint equals the drawn height for a one-paragraph quote, a paragraph with a list, and a long file capped at the bound. Switching to Source, reloading with Source remembered, and a revision rewriting a quote open on Source move nothing below the document. A thread message holding a quote opens at its fitted height and stays there.

Tests

  • test_a_written_markdown_file_is_delivered_rendered_as_a_file (model): delivery writes the rendering beside the <pre>.
  • "an arriving written Markdown file first paints its rendering beside its exact source" (runtime): goes through importWidgets and fails without the prepaint call.
  • test_a_written_markdown_source_keeps_the_preview_height_until_shown_whole (browser): the document's height and the paragraph after it hold across a switch to Source and a reload on Source, and the press reveals every line and focuses the listing. It fails without holdSource.

The revision call site in version.js is covered only by a probe of a live rewrite, not by a test.

This was written by Claude Code on behalf of max-sixty

#4861 sent every quoted Markdown passage to lf-text-document, where it sat
in the fixed box a bound document keeps: a one-paragraph rule drew a 530px
frame around 156px of text, and no eval author set --lf-bound. The box is
fixed because bound text arrives after first paint. A written passage's
text is in the markup, so its rendering can be on the first paint.

A new registry key, `x-prepaint`, names the attribute giving the notation
of an element's written <pre>. Where it names `markdown`, delivery
(`mark_declared`) paints the text as a Markdown file, soft breaks as
spaces, in a `.lf-markdown-body` beside the <pre>, as it already does for
`x-text-format: markdown` words. lf-text-document declares it on
`preview`. Its placeholder shows that rendering in the page's type under
the tab strip's room, and the drawn preview and Source tabs each take
their own height up to --lf-bound. First paint and drawing now measure the
same in Chromium and Firefox (238, 308 and a capped 530px for a paragraph,
a paragraph with a list, and a long file), and switching tabs reflows what
follows, as a change the reader asked for.

This also clears the keeps-first-box finding #4861 left on the default
package's x-example (532px before upgrade, 525px drawn). Bound documents,
and written ones without a Markdown preview, keep the fixed box.
# Conflicts:
#	src/leaf/passages.py
The fitted preview's first box came from the server's rendering, which
only the first document carries. A revision that rewrote a quote, and a
thread message holding one, painted in the fixed 28rem placeholder for
two frames and then shrank to the fitted height: a live rewrite moved
the page below it down by the placeholder's extra height and back, and
a message opened 707px tall and collapsed to 385px while it faded in.

- `prepaintDeclaredFiles` (markdown.js) writes the same rendering in the
  browser, as `formatDeclaredMarkdown` already does for `x-text-format`.
  A revision's arrivals and `importWidgets` call it before connection,
  and `loadDeclaredMarkdown` loads the parser where a file still needs
  its rendering. A version's painted source leaves it out, as its
  baseline does, since block comparison keys a `.lf-markdown-body` by
  its source words.
- The first paint wears the drawn widget's grounds: the figure's paper
  under the caption and the tabs' card from the strip down, clipped at
  the frame's rounded corners. It painted paper there before, and the
  tint changed when the module drew.
- The rendering takes the page's type with scripts off too, where it
  stands in place of the `<pre>`; it showed in the source face.
- `browser/types/contract.d.ts` carries `x-prepaint`, which the last
  commit's schema added without a rebuild.

A document the reader left on Source in the same tab still first paints
at its preview's height and takes its source's when it draws, after a
reload (85px) or a revision that replaces it (75px for two frames on
the fixture). The reader's remembered tab is per-tab browser state that
neither the server nor the arrival can see without naming the widget's
tabs, so that path keeps no box of its own; the module header and the
`x-prepaint` description say so.

Measured with the reviewer's journeys, Chromium unless noted: a message
opens at 210px and stays; a live rewrite on Preview moves what follows
only by the line the revision added (238 to 265px), where it passed
through the 532px placeholder before; first paint equals drawn for one-paragraph, list and capped
quotes in Chromium and Firefox (238, 308.3/309.4, 530.4px).

Tests: the new runtime test "an arriving written Markdown file first
paints its rendering beside its exact source" goes through
`importWidgets`, and fails without its prepaint call. The revision call
site is covered only by the probe above.
The first-paint ground rule matched every undrawn document with a
preview, so a bound or HTML-preview document's placeholder turned from
paper to near-white when it drew, where main's went from white to
near-white. Key it on the server's or browser's rendering instead.

> _This was written by Claude Code on behalf of max-sixty_
# Conflicts:
#	dist/kernel/kernel.json
# Conflicts:
#	dist/kernel/kernel.json
#	dist/kernel/runtime/bundle-data.js
#	tests/browser/widgets/test_code_and_documents.py
# Conflicts:
#	browser/types/contract.d.ts
#	dist/kernel/kernel.json
# Conflicts:
#	browser/types/contract.d.ts
#	dist/kernel/kernel.json
A written Markdown preview fits its rendered text, and its Source tab took
its own height. A quote the reader left on Source then moved the page when it
drew: 48 to 85px on reload, and 74px for two frames when a revision rewrote
it, since its first paint is the preview's rendering.

The Source tab now takes the preview's height: measured while the preview
shows, or, for a document drawn on Source, the box the page first painted.
Where that height cuts the source, its last lines fade under a "Show all N
lines" press, an inline press as a thread's Full quote is, which shows every
line and moves focus to the listing. Switching tabs, reloading on Source and
a revision rewriting a quote left on Source move nothing in Chromium and
Firefox. A cut source no longer scrolls inside its box; the press is the one
way to the rest.

The closed preview isn't laid out to be measured: toggling its
content-visibility for a reading is a write that changes nothing, which the
suite refuses, and laying it out unseen hid it from find-in-page.

A new browser test holds both tabs' height across a switch and a reload on
Source, and the press's reveal and focus.
# Conflicts:
#	packages/default/widgets/lf-text-document.js
@max-sixty
max-sixty merged commit 0d64c00 into main Nov 3, 2026
4 checks passed
@max-sixty
max-sixty deleted the diagnose-markdown-excerpts branch November 3, 2026 20:50
max-sixty added a commit that referenced this pull request Nov 3, 2026
…ge per document (#5226)

A page with one 4 MB `lf-text-document` and 29 empty ones in side tabs
took 7.5–10.4 s to settle in Firefox, against 1.4–2.1 s with the empty
documents removed. Every text document now draws from one shared copy of
Pierre's stylesheet, and the same page settles in 1.6–2.4 s.

## Cause

The Firefox Profiler (`MOZ_PROFILER_STARTUP`) put 5.7 s of the 7.7 s
load in `refit` → `lineColumns` → `Element.clientWidth`, each read
forcing a style flush. The profiler's Styles markers showed about 30
flushes of about 36,000 elements each: the whole page, rows inside the
long document's skipped `content-visibility: auto` runs included.

Pierre's static renderer writes two `<style>` elements into every file
it renders, and `lf-text-document` kept them in each listing, wrapped in
an implicit `@scope`. Firefox 157 restyles the whole document whenever a
`<style>` holding `@scope` enters or leaves it. Timed in the settled
page, inserting one such `<style>` that matched nothing cost a 110–370
ms flush, against 2–9 ms for the same rule without `@scope`. So each
document that drew restyled every element of the page, and each restyle
cost more as copies piled up (about 370 ms with 60 copies on the page,
110 ms with one). Chromium 156 showed only 0.7 s against 0.4 s; it
likely skips styling inside skipped `content-visibility` boxes, which
wasn't measured.

## Change

`sourceListing` now takes Pierre's `<style>` elements out of the
rendered listing and builds one `CSSStyleSheet` from the first listing's
text, scoped by `@scope (.lf-text-document-listing)` in place of the
implicit scope. `show` adopts that sheet into the document or shadow
root the widget stands in, once. Pierre's sheets depend only on the
syntax theme and the options `sourceListing` passes, so every listing on
a page gets the same text. Each listing also no longer carries its own
32 kB of CSS in the DOM.

## Evidence

Built with `build.py` from #4781's re-measure (30 `lf-text-document`s in
`lf-tabs list="side"`, `d0` bound to a 4 MB source, `d1`–`d29` bound to
sources with no data; the control is the single bound document),
exported, and loaded in headless Firefox 157 by a script that waits for
the tab list to be visible, `#d0 code` to show text, and
`body[data-lf-data-version]`. Base and fix exports alternated on the
same machine, three rounds:

| | settled, base | settled, fix |
| --- | --- | --- |
| 30 documents, 29 empty | 7.91, 7.95, 7.79 s | 1.96, 2.17, 1.91 s |
| 1 document (control) | 2.08, 1.82, 1.69 s | 1.88, 1.83, 1.63 s |

The long document's text first showed at 7.56–7.86 s on base against
1.69–2.16 s on the fix, so the empty documents held its first draw back
about 6 s. The tab list's first appearance varied with scheduling in
both versions, at about 0.3 s or with that first draw, so it isn't the
measure here. In Chromium 156 the 30-document page settled in 0.72 and
0.77 s on base against 0.50 and 0.49 s on the fix, with peak renderer
memory falling from 606–617 MB to 430 MB.

After merging main, whose #5223 and #5241 changed this widget's drawing,
two more alternated Firefox rounds on a busier machine settled the
30-document page in 9.84 and 9.36 s on main against 2.21 and 2.08 s on
this branch.

The computed value of every property, on the first 400 elements of the
bound listing and every element of an empty one, is identical before and
after the change, in Chromium and in Firefox once its font list has
loaded.

## Tests

- `test_every_document_draws_from_one_copy_of_pierres_sheet` (new):
three documents in tabs, one with text and two bound to empty sources.
The page holds one copy of Pierre's sheet, and each document's first
line number stands beside its line. It fails on main with three copies.
With the adoption removed it fails with no copy, and with the count
check skipped the line numbers stand 60 px above their lines.
- Ran `tests/browser/widgets/test_code_and_documents.py`, the
text-document tests in `test_render_gate.py`, `test_render_export.py`,
`test_rings.py`, `threads/test_targets.py`,
`startup/test_projected_data.py`, `test_workspaces_and_bounds.py` and
`projection/test_code_review.py` (all passed),
`tests/runtime/text-document-lines.test.mjs`, the browser type check
(`tsc --project browser/types/tsconfig.json`), and `leaf page check
--render` on a five-document page.

No cost test: the load time depends on the machine and engine, and the
new test holds the mechanism, one sheet per page, instead.

## Not verified

- Safari/WebKit, and Firefox on Linux or Windows.
- A document moved into another shadow root after it drew keeps drawing
without Pierre's sheet until its text changes, since adoption happens
when it draws. No Leaf path that moves a drawn document across roots
turned up, but none was searched for exhaustively.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants