Fix toPlainText representation with formatting spans

This commit is contained in:
Jorge Martín 2025-12-19 16:15:51 +01:00
parent 5809901391
commit 0b291ce202
2 changed files with 11 additions and 4 deletions

View file

@ -55,8 +55,15 @@ private class PlainTextNodeVisitor : NodeVisitor {
private val builder = StringBuilder()
override fun head(node: Node, depth: Int) {
if (node is TextNode && node.text().isNotBlank()) {
builder.append(node.text())
if (node is TextNode) {
// If the text node is blank, only add a single whitespace char if there wasn't already one
if (node.text().isBlank()) {
if (builder.lastOrNull()?.isWhitespace() == false) {
builder.append(" ")
}
} else {
builder.append(node.text())
}
} else if (node is Element && node.tagName() == "li") {
val index = node.elementSiblingIndex() + 1
val isOrdered = node.parent()?.nodeName()?.lowercase() == "ol"

View file

@ -45,7 +45,7 @@ class ToPlainTextTest {
val formattedBody = FormattedBody(
format = MessageFormat.HTML,
body = """
Hello world
Hello <strong>formatted</strong> <em>world</em>
<ul><li>This is an unordered list.</li></ul>
<ol><li>This is an ordered list.</li></ol>
<br />
@ -53,7 +53,7 @@ class ToPlainTextTest {
)
assertThat(formattedBody.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo(
"""
Hello world
Hello formatted world
This is an unordered list.
1. This is an ordered list.
""".trimIndent()