diff --git a/CHANGES.md b/CHANGES.md index bb52452f8d..1c348de001 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,6 +22,7 @@ Improvements 🙌: Bugfix 🐛: - Messages encrypted with no way to decrypt after SDK update from 0.18 to 1.0.0 (#2252) - Search Result | scroll jumps after pagination (#2238) + - Badly formatted mentions in body (#1506) Translations 🗣: - diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/LocalEchoEventFactory.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/LocalEchoEventFactory.kt index da3e0429b0..c01923055b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/LocalEchoEventFactory.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/LocalEchoEventFactory.kt @@ -90,8 +90,7 @@ internal class LocalEchoEventFactory @Inject constructor( private fun createTextContent(text: CharSequence, autoMarkdown: Boolean): TextContent { if (autoMarkdown) { - val source = textPillsUtils.processSpecialSpansToMarkdown(text) ?: text.toString() - return markdownParser.parse(source) + return markdownParser.parse(text) } else { // Try to detect pills textPillsUtils.processSpecialSpansToHtml(text)?.let { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParser.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParser.kt index 030c9deb6a..c99d482300 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParser.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/MarkdownParser.kt @@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.session.room.send import org.commonmark.parser.Parser import org.commonmark.renderer.html.HtmlRenderer +import org.matrix.android.sdk.internal.session.room.send.pills.TextPillsUtils import javax.inject.Inject /** @@ -27,18 +28,21 @@ import javax.inject.Inject */ internal class MarkdownParser @Inject constructor( private val parser: Parser, - private val htmlRenderer: HtmlRenderer + private val htmlRenderer: HtmlRenderer, + private val textPillsUtils: TextPillsUtils ) { private val mdSpecialChars = "[`_\\-*>.\\[\\]#~]".toRegex() - fun parse(text: String): TextContent { + fun parse(text: CharSequence): TextContent { + val source = textPillsUtils.processSpecialSpansToMarkdown(text) ?: text.toString() + // If no special char are detected, just return plain text - if (text.contains(mdSpecialChars).not()) { - return TextContent(text) + if (source.contains(mdSpecialChars).not()) { + return TextContent(source) } - val document = parser.parse(text) + val document = parser.parse(source) val htmlText = htmlRenderer.render(document) // Cleanup extra paragraph @@ -48,13 +52,14 @@ internal class MarkdownParser @Inject constructor( htmlText } - return if (isFormattedTextPertinent(text, cleanHtmlText)) { + return if (isFormattedTextPertinent(source, cleanHtmlText)) { // According to https://matrix.org/docs/spec/client_server/latest#m-room-message-msgtypes: // The plain text version of the HTML should be provided in the body. // But it caused too many problems so it has been removed in #2002 - TextContent(text, cleanHtmlText.postTreatment()) + // See #739 + TextContent(text.toString(), cleanHtmlText.postTreatment()) } else { - TextContent(text) + TextContent(source) } }