Merge pull request #6447 from cloudrac3r/fix-html-entities

Fix HTML entities being displayed in messages
This commit is contained in:
Adam Brown 2022-07-04 17:31:34 +01:00 committed by GitHub
commit 2c444527bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

1
changelog.d/6442.bugfix Normal file
View File

@ -0,0 +1 @@
Fix HTML entities being displayed in messages

View File

@ -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("""<span\s+data-mx-maths="([^"]*)">.*?</span>""")) { 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())
}
})