Merge pull request #3522 from vitorpamplona/claude/richtext-trailing-whitespace-85eudl

Strip trailing whitespace from rich text parser output
This commit is contained in:
Vitor Pamplona
2026-07-10 18:11:21 -04:00
committed by GitHub
2 changed files with 52 additions and 2 deletions
@@ -264,7 +264,13 @@ class RichTextParser {
emojis: Map<String, String>,
tags: ImmutableListOfLists<String>,
): ImmutableList<ParagraphState> {
val lines = content.split('\n')
// Trailing spaces and newlines would otherwise produce empty trailing
// paragraphs, each rendered as a blank line between the last visible
// word and the end of the component.
val trimmedContent = content.trimEnd()
if (trimmedContent.isEmpty()) return persistentListOf()
val lines = trimmedContent.split('\n')
val paragraphSegments = ArrayList<ParagraphState>(lines.size)
lines.forEach { paragraph ->
@@ -4087,11 +4087,55 @@ class RichTextParserTest {
assertTrue(state.mediaList.isEmpty())
assertTrue(state.customEmoji.isEmpty())
assertEquals(
"\nHi,\nhow\n\n\n are you doing?\n",
"\nHi,\nhow\n\n\n are you doing?",
state.paragraphs.joinToString("\n") { it.words.joinToString(" ") { it.segmentText } },
)
}
@Test
fun testTrailingWhitespaceIsRemoved() {
val state =
RichTextParser().parseText("Hi, how are you doing? \n\n \n", EmptyTagList, null)
assertEquals(1, state.paragraphs.size)
assertEquals(
"Hi, how are you doing?",
state.paragraphs
.firstOrNull()
?.words
?.firstOrNull()
?.segmentText,
)
}
@Test
fun testTrailingNewLinesAfterImageAreRemoved() {
val state =
RichTextParser().parseText(
"Check this out\nhttps://cdn.nostr.build/image.jpg\n\n\n",
EmptyTagList,
null,
)
assertEquals(2, state.paragraphs.size)
val lastWord =
state.paragraphs
.last()
.words
.single()
assertEquals(
"Image(https://cdn.nostr.build/image.jpg)",
"${lastWord::class.simpleName!!.replace("Segment", "")}(${lastWord.segmentText})",
)
}
@Test
fun testWhitespaceOnlyContentProducesNoParagraphs() {
val state = RichTextParser().parseText(" \n \n ", EmptyTagList, null)
assertTrue(state.paragraphs.isEmpty())
}
@Test
fun testMultiLine() {
val text =