From 592f890fac71423080ebebe647b90dcd8a17fd36 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 22 Jun 2022 02:14:49 +1200 Subject: [PATCH 1/3] Fix formatted_body being parsed as Markdown Background: Clients write Markdown and convert it to HTML before sending the event. All events are formatted as HTML. However, if an HTML formatted event happened to include markdown characters, Element Android would incorrectly render that markdown. For example, an event with formatted_body: "*test*" should be displayed as literally *test* with no effects, but Element Android incorrectly displayed it as test in italics. This commit fixes this behaviour, making Element Android not parse Markdown in HTML messages. From the perspective of most users it will appear that backslash escapes now work properly (even though this wasn't the real issue). --- changelog.d/6357.bugfix | 1 + .../app/features/html/EventHtmlRenderer.kt | 23 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 changelog.d/6357.bugfix diff --git a/changelog.d/6357.bugfix b/changelog.d/6357.bugfix new file mode 100644 index 0000000000..231c65030f --- /dev/null +++ b/changelog.d/6357.bugfix @@ -0,0 +1 @@ +Fix backslash escapes in formatted messages diff --git a/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt b/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt index 7d78be3584..4f7bf8fd1a 100644 --- a/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt +++ b/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt @@ -26,12 +26,17 @@ import im.vector.app.features.settings.VectorPreferences import io.noties.markwon.AbstractMarkwonPlugin import io.noties.markwon.Markwon import io.noties.markwon.MarkwonPlugin +import io.noties.markwon.MarkwonPlugin.Registry import io.noties.markwon.PrecomputedFutureTextSetterCompat import io.noties.markwon.ext.latex.JLatexMathPlugin import io.noties.markwon.ext.latex.JLatexMathTheme import io.noties.markwon.html.HtmlPlugin +import io.noties.markwon.inlineparser.HtmlInlineProcessor +import io.noties.markwon.inlineparser.MarkwonInlineParser import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin +import java.util.Collections import org.commonmark.node.Node +import org.commonmark.parser.Parser import timber.log.Timber import javax.inject.Inject import javax.inject.Singleton @@ -63,14 +68,28 @@ class EventHtmlRenderer @Inject constructor( } } }) - .usePlugin(MarkwonInlineParserPlugin.create()) .usePlugin(JLatexMathPlugin.create(44F) { builder -> builder.inlinesEnabled(true) builder.theme().inlinePadding(JLatexMathTheme.Padding.symmetric(24, 8)) }) } else { builder - }.textSetter(PrecomputedFutureTextSetterCompat.create()).build() + } + .usePlugin(MarkwonInlineParserPlugin.create(MarkwonInlineParser.factoryBuilderNoDefaults())) + .usePlugin(object : AbstractMarkwonPlugin() { + override fun configure(registry: Registry) { + registry.require(MarkwonInlineParserPlugin::class.java, { plugin: MarkwonInlineParserPlugin -> + plugin.factoryBuilder().addInlineProcessor(HtmlInlineProcessor()) + }) + } + }) + .usePlugin(object : AbstractMarkwonPlugin() { + override fun configureParser(builder: Parser.Builder) { + builder.enabledBlockTypes(Collections.emptySet()) + } + }) + .textSetter(PrecomputedFutureTextSetterCompat.create()) + .build() val plugins: List = markwon.plugins From 216de6c0508eec791d2e0a49eae4a08c3816bdf0 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 22 Jun 2022 13:10:30 +1200 Subject: [PATCH 2/3] Lint (import ordering) --- .../main/java/im/vector/app/features/html/EventHtmlRenderer.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt b/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt index 4f7bf8fd1a..39fb3fa804 100644 --- a/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt +++ b/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt @@ -34,10 +34,10 @@ import io.noties.markwon.html.HtmlPlugin import io.noties.markwon.inlineparser.HtmlInlineProcessor import io.noties.markwon.inlineparser.MarkwonInlineParser import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin -import java.util.Collections import org.commonmark.node.Node import org.commonmark.parser.Parser import timber.log.Timber +import java.util.Collections import javax.inject.Inject import javax.inject.Singleton From e940736938f3a093a825f2e466f9a4bf13876e11 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 25 Jun 2022 15:27:56 +1200 Subject: [PATCH 3/3] Combine plugin creation and configuration As requested in PR feedback. --- .../app/features/html/EventHtmlRenderer.kt | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt b/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt index 39fb3fa804..48d2c1b685 100644 --- a/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt +++ b/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt @@ -26,7 +26,6 @@ import im.vector.app.features.settings.VectorPreferences import io.noties.markwon.AbstractMarkwonPlugin import io.noties.markwon.Markwon import io.noties.markwon.MarkwonPlugin -import io.noties.markwon.MarkwonPlugin.Registry import io.noties.markwon.PrecomputedFutureTextSetterCompat import io.noties.markwon.ext.latex.JLatexMathPlugin import io.noties.markwon.ext.latex.JLatexMathTheme @@ -37,7 +36,6 @@ import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin import org.commonmark.node.Node import org.commonmark.parser.Parser import timber.log.Timber -import java.util.Collections import javax.inject.Inject import javax.inject.Singleton @@ -75,17 +73,14 @@ class EventHtmlRenderer @Inject constructor( } else { builder } - .usePlugin(MarkwonInlineParserPlugin.create(MarkwonInlineParser.factoryBuilderNoDefaults())) - .usePlugin(object : AbstractMarkwonPlugin() { - override fun configure(registry: Registry) { - registry.require(MarkwonInlineParserPlugin::class.java, { plugin: MarkwonInlineParserPlugin -> - plugin.factoryBuilder().addInlineProcessor(HtmlInlineProcessor()) - }) - } - }) + .usePlugin( + MarkwonInlineParserPlugin.create( + MarkwonInlineParser.factoryBuilderNoDefaults().addInlineProcessor(HtmlInlineProcessor()) + ) + ) .usePlugin(object : AbstractMarkwonPlugin() { override fun configureParser(builder: Parser.Builder) { - builder.enabledBlockTypes(Collections.emptySet()) + builder.enabledBlockTypes(kotlin.collections.emptySet()) } }) .textSetter(PrecomputedFutureTextSetterCompat.create())