From 0fbef88c2d87c2c58bd623e739774775a9d7d328 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 31 Oct 2022 18:25:38 +0000 Subject: [PATCH] adding support for blockquotes --- .../sync/internal/sync/message/HtmlParser.kt | 19 +++++++++++++++++++ .../sync/internal/sync/message/PartBuilder.kt | 2 -- .../internal/sync/RichMessageParserTest.kt | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/message/HtmlParser.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/message/HtmlParser.kt index 1f132b3..e43ab24 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/message/HtmlParser.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/message/HtmlParser.kt @@ -99,6 +99,25 @@ internal class HtmlParser { exitTagCloseIndex } + "blockquote" -> { + if (tagContent.isNotEmpty() && nestingLevel < 3) { + var lastIndex = 0 + val trimmedTagContent = tagContent.trim() + builder.appendText("> ") + iterateSearchIndex { searchIndex -> + lastIndex = searchIndex + parseHtmlTags(trimmedTagContent, searchIndex, builder, nestingLevel = nestingLevel + 1) + } + + if (lastIndex < trimmedTagContent.length) { + builder.appendText(trimmedTagContent.substring(lastIndex)) + } + } + + builder.appendNewline() + exitTagCloseIndex + } + "p" -> { if (tagContent.isNotEmpty() && nestingLevel < 2) { var lastIndex = 0 diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/message/PartBuilder.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/message/PartBuilder.kt index abc30ae..2cbf4e1 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/message/PartBuilder.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/message/PartBuilder.kt @@ -10,8 +10,6 @@ internal class PartBuilder { private val parts = mutableListOf() fun appendText(value: String) { - println("append text") - normalBuffer.append(value.cleanFirstTextLine()) } diff --git a/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RichMessageParserTest.kt b/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RichMessageParserTest.kt index 72b07a2..e910a07 100644 --- a/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RichMessageParserTest.kt +++ b/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RichMessageParserTest.kt @@ -84,6 +84,11 @@ class RichMessageParserTest { expected = RichText(listOf(Normal("Hello world!\nnext line\nanother line"))) ) + @Test + fun `parses blockquote tags`() = runParserTest( + input = "
\n

hello world

\n
\n", + expected = RichText(listOf(Normal("> "), Bold("hello"), Normal(" "), Italic("world"))) + ) @Test fun `parses lists`() = runParserTest(