Fix toPlainText representation with formatting spans
This commit is contained in:
parent
2ca981ddfa
commit
02b6a90a95
2 changed files with 11 additions and 4 deletions
|
|
@ -55,8 +55,15 @@ private class PlainTextNodeVisitor : NodeVisitor {
|
||||||
private val builder = StringBuilder()
|
private val builder = StringBuilder()
|
||||||
|
|
||||||
override fun head(node: Node, depth: Int) {
|
override fun head(node: Node, depth: Int) {
|
||||||
if (node is TextNode && node.text().isNotBlank()) {
|
if (node is TextNode) {
|
||||||
builder.append(node.text())
|
// 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") {
|
} else if (node is Element && node.tagName() == "li") {
|
||||||
val index = node.elementSiblingIndex() + 1
|
val index = node.elementSiblingIndex() + 1
|
||||||
val isOrdered = node.parent()?.nodeName()?.lowercase() == "ol"
|
val isOrdered = node.parent()?.nodeName()?.lowercase() == "ol"
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ class ToPlainTextTest {
|
||||||
val formattedBody = FormattedBody(
|
val formattedBody = FormattedBody(
|
||||||
format = MessageFormat.HTML,
|
format = MessageFormat.HTML,
|
||||||
body = """
|
body = """
|
||||||
Hello world
|
Hello <strong>formatted</strong> <em>world</em>
|
||||||
<ul><li>This is an unordered list.</li></ul>
|
<ul><li>This is an unordered list.</li></ul>
|
||||||
<ol><li>This is an ordered list.</li></ol>
|
<ol><li>This is an ordered list.</li></ol>
|
||||||
<br />
|
<br />
|
||||||
|
|
@ -53,7 +53,7 @@ class ToPlainTextTest {
|
||||||
)
|
)
|
||||||
assertThat(formattedBody.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo(
|
assertThat(formattedBody.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo(
|
||||||
"""
|
"""
|
||||||
Hello world
|
Hello formatted world
|
||||||
• This is an unordered list.
|
• This is an unordered list.
|
||||||
1. This is an ordered list.
|
1. This is an ordered list.
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue