Fix toPlainText where <ol start='n'> tags appear (#5044)

This commit is contained in:
Jorge Martin Espinosa 2025-07-18 13:23:26 +02:00 committed by GitHub
parent 7f65075bbd
commit 760ff51061
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View file

@ -57,10 +57,16 @@ private class PlainTextNodeVisitor : NodeVisitor {
if (node is TextNode && node.text().isNotBlank()) {
builder.append(node.text())
} else if (node is Element && node.tagName() == "li") {
val index = node.elementSiblingIndex()
val index = node.elementSiblingIndex() + 1
val isOrdered = node.parent()?.nodeName()?.lowercase() == "ol"
if (isOrdered) {
builder.append("${index + 1}. ")
val startIndex = node.parent()?.attr("start")?.toIntOrNull()
val actualIndex = if (startIndex != null) {
startIndex + index - 1
} else {
index
}
builder.append("$actualIndex. ")
} else {
builder.append("")
}

View file

@ -96,6 +96,29 @@ class ToPlainTextTest {
)
}
@Test
fun `TextMessageType toPlainText - respects the ol start attr if present`() {
val messageType = TextMessageType(
body = "1. First item\n2. Second item\n",
formatted = FormattedBody(
format = MessageFormat.HTML,
body = """
<ol start='11'>
<li>First item.</li>
<li>Second item.</li>
</ol>
<br />
""".trimIndent()
)
)
assertThat(messageType.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo(
"""
11. First item.
12. Second item.
""".trimIndent()
)
}
@Test
fun `TextMessageType toPlainText - returns the markdown body if the formatted one cannot be parsed`() {
val messageType = TextMessageType(