Markdown body should be pure text, see #739, #1506.

This commit is contained in:
Onuray Sahin 2020-11-03 11:53:50 +03:00
parent 734602f4f7
commit b40334f7db
3 changed files with 15 additions and 10 deletions

View File

@ -22,6 +22,7 @@ Improvements 🙌:
Bugfix 🐛: Bugfix 🐛:
- Messages encrypted with no way to decrypt after SDK update from 0.18 to 1.0.0 (#2252) - 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) - Search Result | scroll jumps after pagination (#2238)
- Badly formatted mentions in body (#1506)
Translations 🗣: Translations 🗣:
- -

View File

@ -90,8 +90,7 @@ internal class LocalEchoEventFactory @Inject constructor(
private fun createTextContent(text: CharSequence, autoMarkdown: Boolean): TextContent { private fun createTextContent(text: CharSequence, autoMarkdown: Boolean): TextContent {
if (autoMarkdown) { if (autoMarkdown) {
val source = textPillsUtils.processSpecialSpansToMarkdown(text) ?: text.toString() return markdownParser.parse(text)
return markdownParser.parse(source)
} else { } else {
// Try to detect pills // Try to detect pills
textPillsUtils.processSpecialSpansToHtml(text)?.let { textPillsUtils.processSpecialSpansToHtml(text)?.let {

View File

@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.session.room.send
import org.commonmark.parser.Parser import org.commonmark.parser.Parser
import org.commonmark.renderer.html.HtmlRenderer import org.commonmark.renderer.html.HtmlRenderer
import org.matrix.android.sdk.internal.session.room.send.pills.TextPillsUtils
import javax.inject.Inject import javax.inject.Inject
/** /**
@ -27,18 +28,21 @@ import javax.inject.Inject
*/ */
internal class MarkdownParser @Inject constructor( internal class MarkdownParser @Inject constructor(
private val parser: Parser, private val parser: Parser,
private val htmlRenderer: HtmlRenderer private val htmlRenderer: HtmlRenderer,
private val textPillsUtils: TextPillsUtils
) { ) {
private val mdSpecialChars = "[`_\\-*>.\\[\\]#~]".toRegex() 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 no special char are detected, just return plain text
if (text.contains(mdSpecialChars).not()) { if (source.contains(mdSpecialChars).not()) {
return TextContent(text) return TextContent(source)
} }
val document = parser.parse(text) val document = parser.parse(source)
val htmlText = htmlRenderer.render(document) val htmlText = htmlRenderer.render(document)
// Cleanup extra paragraph // Cleanup extra paragraph
@ -48,13 +52,14 @@ internal class MarkdownParser @Inject constructor(
htmlText 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: // 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. // 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 // 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 { } else {
TextContent(text) TextContent(source)
} }
} }