Skip to content

Add an option that makes a newline in a paragraph a line break - #34

Merged
servercimen merged 11 commits into
feature/style-optionsfrom
feature/soft-line-breaks
Nov 24, 2026
Merged

servercimen merged 11 commits into
feature/style-optionsfrom
feature/soft-line-breaks

Conversation

@servercimen

Copy link
Copy Markdown
Owner

Part 4 of a stack of 13 pull requests. The base is #33 (feature/style-options). #31 (part 1) tells how to merge the stack.

On this stacked branch, swift test passes (223 tests), and the iOS build and build-for-testing pass.

Summary

Models often use a single newline as a line break. CommonMark makes it a space, so an app that shows model output joins lines that the model meant to keep apart. This change adds the option newlinesAsLineBreaks. The default is false, so the parser follows CommonMark as before.

  • Add newlinesAsLineBreaks: Bool = false to MarkdownStreamParser.init, MarkdownStreamParser.document(from:) and TranscriptStream.init. With the option, a newline inside a paragraph or a heading is a line break, also in lists and quotes. A backslash, two spaces or <br> at the end of a line still give one break, not two. A newline in a code span stays a space. Code blocks, tables and display math do not change.
  • Read a link destination that starts after a line break. Before, [a](, a newline and a URL gave an empty destination. With the option off, this changes one result: [a](, a backslash, a newline and a URL now give a link.
  • Run the incremental parser tests with the option on and off, on a new text with many kinds of line ends. Add LineBreakTests.swift, and add rows with the option to the parser timing test.
  • Add a "Line breaks" section to the README.

Tests

  • swift test on macOS: 193 tests pass.
  • xcodebuild build -scheme TranscriptKit -destination 'generic/platform=iOS' CODE_SIGNING_ALLOWED=NO: passes.

Checked on an iPhone 12 mini

A check screen in a local test app ran on main first (3 runs) and showed the problem. Then it ran on this branch (5 passing runs). View A has the option, and view B has the default. Stand-ins: the text comes from code in 3-character chunks every 30 ms, not from a model. The check copies with selectAll and copy from code. This device does not support screen recording, so the frames come from layer.render(in:) at each display refresh.

  • Main: a single newline is a space in the paragraph, the list item and the quote. The text has 5 lines (139 pt). Copy gives spaces.
  • Branch, view A: each newline starts a new line (8 lines, 211 pt). The second lines of the list item and the quote align with their first lines. Copy gives the newlines.
  • Branch, view A while it streams: no space shows at a break, and the carets do not move. The height only grows. The tail shows the end of the text in 118 of 120 frames.
  • Branch, view B: every value is the same as on main.
  • one\ntwo\nthree with the option is 69 pt (3 lines), the same as with hard breaks. On main it is 21 pt (1 line). A <br>, a backslash or two spaces before a newline give one break each.
  • Parser time for 17379 characters, median of 5 in each run: main 97 to 104 ms, branch 96 to 105 ms, branch with the option 96 to 100 ms.

Not checked

  • A screen recording of the stream.
  • Select and copy with a finger and the edit menu.

Known limits

  • With the option off, two spaces at the end of a line do not make a line break. This is the same on main. This change does not change it.

🤖 Generated with Claude Code

servercimen and others added 10 commits November 23, 2026 17:19
Models often use a single newline as a line break. CommonMark makes it a
space. Add newlinesAsLineBreaks to MarkdownStreamParser.init,
MarkdownStreamParser.document(from:) and TranscriptStream.init. The
default is false, so the parser follows CommonMark as before.

With the option, the block parser puts a line break, not a space, between
two lines of a paragraph or a heading. A line that ends in a backslash or
two spaces still gives one line break. The text that text(in:) and
copyText(in:) give has a newline at the break.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The inline parser trimmed only spaces and tabs around a link destination.
When a line break came after the "(", the destination was empty. This
occurs for [a](\nurl) with the new option, and for a backslash line
break without it. Trim newlines too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Run the chunk-split test, the frozen-block test and the test that the
shown text only grows with the option on and off. Add a text with
single-newline paragraphs, hard breaks, lists, quotes, a setext heading,
CRLF line ends and lines that could start other blocks. Check that the
inline parse of a paragraph of many lines resumes near its end with the
option.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With newlinesAsLineBreaks, the inline parser now gets the option too.

- A newline right after <br> adds no second line break. A line that
  ends in <br> gives one break, as a backslash or two spaces do.
- A newline in a code span is a space, as in CommonMark.
- A built-in tag such as <b> or <br /> can go over two lines.

The output with the option off does not change. The streamed text
corpus now has these cases, so the chunk-split tests cover them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The TranscriptStream doc now says "a paragraph or a heading", as the
MarkdownStreamParser doc does. The README says that <br> at the end of
a line gives one break, and that a newline in a code span is a space.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A closing </br> is a tag only when an app adds a custom tag named br.
The parser does not look past it, so it must not drop the newline that
follows it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@servercimen
servercimen added this pull request to stack #44 November 24, 2026 20:57
@servercimen
servercimen removed this pull request from stack #44 November 24, 2026 21:01
@servercimen
servercimen added this pull request to stack #45 November 24, 2026 21:01
@servercimen
servercimen merged commit 431e980 into main Nov 24, 2026
0 of 3 checks passed
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.

1 participant