handle headers as bold text

This commit is contained in:
Adam Brown 2022-10-29 10:33:56 +01:00
parent 55745b9c41
commit 7016f7c4ee
3 changed files with 24 additions and 17 deletions

View File

@ -73,7 +73,7 @@ internal class HtmlParser {
}
}
"b" -> {
"b", "strong" -> {
builder.appendBold(tagContent)
exitTagCloseIndex
}
@ -81,21 +81,16 @@ internal class HtmlParser {
"p" -> {
builder.appendText(tagContent)
builder.appendNewline()
exitTagCloseIndex
}
"h1", "h2", "h3", "h4", "h5" -> {
builder.appendBold(tagContent)
builder.appendNewline()
exitTagCloseIndex
}
"strong" -> {
builder.appendBold(tagContent)
exitTagCloseIndex
}
"i" -> {
builder.appendItalic(tagContent)
exitTagCloseIndex
}
"em" -> {
"i", "em" -> {
builder.appendItalic(tagContent)
exitTagCloseIndex
}

View File

@ -37,6 +37,14 @@ internal class PartBuilder {
fun build(): Set<RichText.Part> {
flushNormalBuffer()
val last = parts.last()
if (last is RichText.Part.Normal) {
parts.remove(last)
val newContent = last.content.trimEnd()
if (newContent.isNotEmpty()) {
parts.add(last.copy(content = newContent))
}
}
return parts
}

View File

@ -21,7 +21,7 @@ class RichMessageParserTest {
@Test
fun `parses p tags`() = runParserTest(
input = "<p>Hello world!</p><p>foo bar</p>after paragraph",
expected = RichText(setOf(Normal("Hello world!\n\nfoo bar\n\nafter paragraph")))
expected = RichText(setOf(Normal("Hello world!\nfoo bar\nafter paragraph")))
)
@Test
@ -49,18 +49,22 @@ class RichMessageParserTest {
)
@Test
fun `skips header tags`() = runParserTest(
fun `parses header tags`() = runParserTest(
Case(
input = "<h1>hello</h1>",
expected = RichText(setOf(Normal("hello")))
expected = RichText(setOf(Bold("hello")))
),
Case(
input = "<h1>hello</h1>text after title",
expected = RichText(setOf(Bold("hello"), Normal("\ntext after title")))
),
Case(
input = "<h2>hello</h2>",
expected = RichText(setOf(Normal("hello")))
expected = RichText(setOf(Bold("hello")))
),
Case(
input = "<h3>hello</h3>",
expected = RichText(setOf(Normal("hello")))
expected = RichText(setOf(Bold("hello")))
),
)