Skip to content

Read a draft as a Markdown file, so a wrapped passage wraps to its box - #4872

Merged
max-sixty merged 7 commits into
mainfrom
markdown-preview-wrapping
Nov 1, 2026
Merged

max-sixty merged 7 commits into
mainfrom
markdown-preview-wrapping

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

A draft (lf-draft, the one widget with x-text-format: markdown) drew a line break at each of its source's, as a typed message does. A passage an agent copied from a file hard-wrapped at 88 columns therefore showed a short line after each long one in a narrower box. #4848 turned those breaks off for the Markdown preview and kept them for drafts ("Drafts still keep their source breaks"). The user reversed that for drafts on 2026-10-31: a draft now reads as a Markdown file does on GitHub, where a line break inside a paragraph is a space.

  • The body (paintMarkdown), Python's prepaint and passage reading (block_markdown, markdown_markup) render soft breaks as spaces. Messages keep their typed breaks.
  • The draft's editor joins those lines in place (joints in field-editor.js). Every line break among a paragraph's words, a code span's and a link destination's included, draws as one space the caret steps over, with the trailing spaces before it and the next line's markers and indent. A hard break's line stays a line. So no word moves when the editor opens ("Editing in place").
  • Return in a draft begins a line the body shows too, carrying the markers of the quotes and items around the caret. Among a paragraph's words it writes a hard break in place of the spaces, or the joined break, at the caret; at a hard break it begins the line after it. After a paragraph's last words it begins a new paragraph, and in a list item the next item. Before any words in a quote or item it continues that container, and on a bare line it leaves it. Shift+Enter does the same in a draft, since a line it continued would join the paragraph above.
  • The draft's reading moved from the preview plugin into a StateField, so the preview, Shift+Enter, Tab and the joints read one copy, and the container helpers continueBlocks used are shared with Return.

A link destination or picture source that itself spans a line break still shows as source in the editor, as before: the preview's plugin can't hide a range across a line break.

Tests:

  • New test_a_wrapped_draft_reads_and_edits_as_one_paragraph: a wrapped paragraph, list item, quote and code span draw no <br>, and their words stand within 1px of the prose in the editor. It fails with the joints off, and with joints only at soft breaks (the code span's word moves a line).
  • test_document_edit_enter_adds_a_line_and_modified_enter_saves_exact_source protected Return as a plain line. One Return after a paragraph's last words now begins a new paragraph ("\n\n", where two were needed). Return between words writes a hard break, and Return at that hard break adds a second; the draft saves with two <br>. The last step fails without the hard-break handling.
  • DRAFT_AS_DRAWN reads a draft with no picture.
  • body-text.test.mjs: the caret test finds "word300" in the paragraph's one text node rather than in a second one after a <br>, and the shared dialect test renders as the body paints, with breaks off.

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

…view

The preview rendered with the runtime's one block dialect, which reads a
newline inside a paragraph as a line break, as a message's typed text means
it. A file hard-wrapped near 88 columns, previewed in a narrower column, then
wrapped twice: once at the column's edge and again at each source newline,
leaving one-word lines such as "but must / not" mid-paragraph.

`renderMarkdown(text, breaks)` now takes the `breaks` choice
`renderInlineMarkdown` already had, sharing one lazily built joined dialect,
and the text-document preview passes `false`, so a newline inside a paragraph
is a space, as on GitHub's file view and in CommonMark. Messages, drafts and
other callers keep the default. `markdownWords` calls `renderMarkdown` so it
keeps the typed reading explicitly.

The document-readings browser test now wraps a list item across two source
lines and expects it to read as one line of words.
…ping

# Conflicts:
#	browser/runtime/markdown.js
#	dist/kernel/kernel.json
#	dist/kernel/runtime/bundle-data.js
#	packages/default/widgets/lf-text-document.js
#	skills/leaf/references/module-reading.md
#	tests/browser/widgets/test_code_and_documents.py
A draft (`lf-draft`, the one widget with `x-text-format: markdown`) drew a line
break at each of its source's, as a typed message does. A passage an agent
copied from a file hard-wrapped at 88 columns therefore showed a short line
after each long one in a narrower box. #4848 turned those breaks off for the
Markdown preview and kept them for drafts ("Drafts still keep their source
breaks"). The user reversed that for drafts on 2026-10-31: a draft now reads
as a Markdown file does on GitHub, where a line break inside a paragraph is a
space.

- The body (`paintMarkdown`), Python's prepaint and passage reading
  (`block_markdown`, `markdown_markup`) render soft breaks as spaces. Messages
  keep their typed breaks.
- The draft's editor joins those lines in place (`joints` in
  field-editor.js). Every line break among a paragraph's words, a code span's
  and a link destination's included, draws as one space the caret steps over,
  with the trailing spaces before it and the next line's markers and indent.
  A hard break's line stays a line. So no word moves when the editor opens
  ("Editing in place").
- Return in a draft begins a line the body shows too, carrying the markers of
  the quotes and items around the caret. Among a paragraph's words it writes
  a hard break in place of the spaces, or the joined break, at the caret.
  After a paragraph's last words it begins a new paragraph, and in a list
  item the next item. Before any words in a quote or item it continues that
  container, and on a bare line it leaves it. Shift+Enter does the same in a
  draft, since a line it continued would join the paragraph above.
- The draft's reading moved from the preview plugin into a StateField, so the
  preview, Shift+Enter and the joints read one copy, and the container
  helpers `continueBlocks` used are shared with Return.

Tests:

- New `test_a_wrapped_draft_reads_and_edits_as_one_paragraph`: a wrapped
  paragraph, list item, quote and code span draw no `<br>`, and their words
  stand within 1px of the prose in the editor. It fails with the joints off,
  and with joints only at soft breaks (the code span's word moves a line).
- `test_document_edit_enter_adds_a_line_and_modified_enter_saves_exact_source`
  protected Return as a plain line. One Return after a paragraph's last words
  now begins a new paragraph ("\n\n", where two were needed), and Return
  between words writes a hard break that saves as one `<br>`.
- `DRAFT_AS_DRAWN` reads a draft with no picture.
- body-text.test.mjs: the caret test finds "word300" in the paragraph's one
  text node rather than in a second one after a `<br>`, and the shared dialect
  test renders as the body paints, with breaks off.
# Conflicts:
#	browser/runtime/composing/field-editor.js
#	dist/kernel/kernel.json
#	dist/kernel/runtime/bundle-page-layout.js
Return between a hard break's backslash and its line's end wrote a second
backslash, which escaped the first, and left the break's own newline as a
blank line, so the paragraph split with a literal "\" shown. A hard break
holding the range's end, with the spaces before it, is a line already, so
the new hard break now goes after it. The Return test adds that step.
A selection from between a hard break's backslash and its newline to past the
break kept the backslash, which then escaped the new one, leaving no break.
The replaced range now starts where that hard break does.
@max-sixty
max-sixty merged commit 761c507 into main Nov 1, 2026
4 checks passed
@max-sixty
max-sixty deleted the markdown-preview-wrapping branch November 1, 2026 00:26
max-sixty pushed a commit that referenced this pull request Nov 2, 2026
Opening a long Markdown draft could reset the document to the top: the
editor initially stood below the reading, then native scroll anchoring
followed it upward when its ready state hid the reading. The reading and
editor now occupy one grid cell throughout that handoff, so clicking a
visible word keeps the reading position and transfers its caret.
Content-sized rows preserve scrolling inside a bounded pane.

The regression starts at #4872: the existing document test passes on its
parent and fails at that commit with the reported 429px reset. That
change intentionally joined Markdown soft breaks; this repair keeps that
behavior and corrects the layout handoff instead. The existing document
and pane cases, wrapped-draft geometry, and draft Return tests pass (5
tests); all 745 runtime tests and the changed files’ pre-commit checks
pass.

### Visual evidence

The same journey writes a long draft, saves it, scrolls to Line 30 and
clicks that word. Before, the document jumps back to the draft’s head;
after, Line 30 stays at the clicked height. The existing short-editor,
hover and phone states are pixel-identical between base and head.

| Before | After |
| --- | --- |
| ![Opening the draft loses the reading
position](https://github.com/user-attachments/assets/0fb6043a-b026-4d9f-b973-670932aad00b)
| ![Opening the draft keeps the clicked line in
place](https://github.com/user-attachments/assets/0ee40126-ae38-4b48-a62f-ba0bb4e8a70b)
|

<details><summary>Verification and scope</summary>

- Reproduction: `uv run pytest
'tests/browser/drafts/test_draft_editing.py::test_opening_a_visible_line_in_a_long_draft_preserves_the_reading_position[document-markdown]'
-q -n0` fails on base and passes with the correction. The unchanged test
checks scroll offsets, the draft’s position, the clicked source caret
and the exact editor value.
- Focused selection: `uv run pytest
tests/browser/drafts/test_draft_editing.py -k 'opening_a_visible_line or
wrapped_draft or document_edit_enter' -q -n0` — 5 passed.
- Captures: `uv run leaf-dev stills 32501dd --state release-draft
--state release-draft-long --state release-draft-hover --state
release-draft-phone`.
- Served startup comparison on `release-notes`: document delivery,
upgrade and first server-state presentation add no requests or
JavaScript bytes (45 requests and 2,326,152 JavaScript bytes before
presentation in both arms). Total bytes rise from 3,024,878 to
3,025,175; code bytes rise by 222 for the CSS. Both arms show the same
pre-upgrade layout-shift diagnostic. Single-run elapsed times are not a
performance conclusion.
- The double-click geometry/caret test reaches its separate print
assertion and fails there on both unchanged base and candidate: a soft
newline is now a space. That failure is already owned by #5075; this PR
changes no print rules or assertions.

</details>

Closes #5083 — automated triage

Co-authored-by: leaf-agent <[email protected]>
leaf-agent added a commit that referenced this pull request Nov 3, 2026
…t it grew 200px (#5210)

`test_generated_page_interface_reconciles_before_semantic_interaction`
has failed on every `ci` run on main since #4872 merged. That PR made a
draft read as Markdown and deliberately joined soft line breaks. The
fixture's replayed draft is sixteen lines joined by single newlines, so
it now renders as one wrapped paragraph. Replay therefore grows the
first section by 136px (authored span 139.19, presented 275.13), short
of the fixed `> authored_span + 200` threshold.

What the test protects still holds: the first presented paint's contents
span matches the live geometry (`span == approx(actual, abs=1)`) rather
than the stale authored span. The growth assertion was only a positive
control for that check, and the 200px figure was arbitrary. It now asks
for exactly what the reconciliation check needs: the replayed geometry
must differ from the authored span by more than the 1px tolerance.
Otherwise a stale span could pass. #4872's behavior and the interface
are untouched.

Verification: the test passes on #4872's parent and fails at #4872 with
the same numbers CI reports (`assert 275.125 > (139.1875 + 200)`). It
fails on current main and passes with this change.

Closes #5164 — automated triage

Co-authored-by: leaf-agent <[email protected]>
leaf-agent added a commit that referenced this pull request Nov 4, 2026
…ests (#5333)

The tests were stale, and the drafts themselves work as intended. #4872
made a draft read as a Markdown file. A line break inside a paragraph
now draws as a space, and Return or Shift+Enter after a paragraph's last
words now starts a new paragraph (`\n\n`). Four browser-tier tests still
expected the old reading, and because pull requests don't run that tier,
they went red on main. This PR updates what those tests expect. It
changes no product code.

- `test_page_round_trip` reads the draft body's text, and so does the
print step of
`test_double_clicking_a_draft_keeps_its_frame_and_selected_word`. Both
now expect `DRAFT_DRAWN`: the source text with its line break drawn as a
space. `DRAFT_TEXT` still names the source, which the editor opens on.
`test_the_draft_box_is_its_own_door` passed only because `to_have_text`
normalizes whitespace. It now reads the same constant.
- In
`test_submit_shortcuts_activate_the_controls_that_promise_the_action`
and `test_touch_return_keeps_newlines_until_the_user_taps_submit`, the
draft half now expects the paragraph break that Return writes. The
comment-box halves keep typed line breaks, and their expectations are
unchanged.

All four failed locally on main with the messages in the issue, and all
five tests named above pass on this branch.

#5075 also recorded
`test_a_reply_written_in_a_card_keeps_its_line_breaks_once_sent[mouse]`.
That failure is a layout difference in card replies, not a difference in
how text is read: the sent message wraps one word earlier than the
editor did. This PR doesn't address it, and #5332 tracks it.

Closes #5075 — automated triage

Co-authored-by: leaf-agent <[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