mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-08-11 08:47:33 +00:00
Merge pull request #3522 from vitorpamplona/claude/richtext-trailing-whitespace-85eudl
Strip trailing whitespace from rich text parser output
This commit is contained in:
+7
-1
@@ -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 ->
|
||||
|
||||
+45
-1
@@ -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 =
|
||||
|
||||
Reference in New Issue
Block a user