diff --git a/changelog.d/6442.bugfix b/changelog.d/6442.bugfix new file mode 100644 index 0000000000..070ff94bee --- /dev/null +++ b/changelog.d/6442.bugfix @@ -0,0 +1 @@ +Fix HTML entities being displayed in 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 48d2c1b685..ed6b68e04a 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 @@ -14,6 +14,15 @@ * limitations under the License. */ +/* + * This file renders the formatted_body of an event to a formatted Android Spannable. + * The core of this work is done with Markwon, a general-purpose Markdown+HTML formatter. + * Since formatted_body is HTML only, Markwon is configured to only handle HTML, not Markdown. + * The EventHtmlRenderer class is next used in the method buildFormattedTextItem + * in the file MessageItemFactory.kt. + * Effectively, this is used in the chat messages view and the room list message previews. + */ + package im.vector.app.features.html import android.content.Context @@ -30,6 +39,7 @@ 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.EntityInlineProcessor import io.noties.markwon.inlineparser.HtmlInlineProcessor import io.noties.markwon.inlineparser.MarkwonInlineParser import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin @@ -54,8 +64,10 @@ class EventHtmlRenderer @Inject constructor( .usePlugin(HtmlPlugin.create(htmlConfigure)) private val markwon = if (vectorPreferences.latexMathsIsEnabled()) { + // If latex maths is enabled in app preferences, refomat it so Markwon recognises it + // It needs to be in this specific format: https://noties.io/Markwon/docs/v4/ext-latex builder - .usePlugin(object : AbstractMarkwonPlugin() { // Markwon expects maths to be in a specific format: https://noties.io/Markwon/docs/v4/ext-latex + .usePlugin(object : AbstractMarkwonPlugin() { override fun processMarkdown(markdown: String): String { return markdown .replace(Regex(""".*?""")) { matchResult -> @@ -75,11 +87,20 @@ class EventHtmlRenderer @Inject constructor( } .usePlugin( MarkwonInlineParserPlugin.create( - MarkwonInlineParser.factoryBuilderNoDefaults().addInlineProcessor(HtmlInlineProcessor()) + /* Configuring the Markwon inline formatting processor. + * Default settings are all Markdown features. Turn those off, only using the + * inline HTML processor and HTML entities processor. + */ + MarkwonInlineParser.factoryBuilderNoDefaults() + .addInlineProcessor(HtmlInlineProcessor()) // use inline HTML processor + .addInlineProcessor(EntityInlineProcessor()) // use HTML entities processor ) ) .usePlugin(object : AbstractMarkwonPlugin() { override fun configureParser(builder: Parser.Builder) { + /* Configuring the Markwon block formatting processor. + * Default settings are all Markdown blocks. Turn those off. + */ builder.enabledBlockTypes(kotlin.collections.emptySet()) } })