Move PermalinkBuilder, MatrixToConverter and PermalinkParser content to the impl project in order to remove projects.appconfig dependency from matrix.api module.
This commit is contained in:
parent
5ee1b8f79b
commit
db3f17fd7d
52 changed files with 771 additions and 318 deletions
|
|
@ -29,9 +29,13 @@ import org.jsoup.nodes.Document
|
|||
*
|
||||
* This will also make sure mentions are prefixed with `@`.
|
||||
*
|
||||
* @param permalinkParser the parser to use to parse the mentions.
|
||||
* @param prefix if not null, the prefix will be inserted at the beginning of the message.
|
||||
*/
|
||||
fun FormattedBody.toHtmlDocument(prefix: String? = null): Document? {
|
||||
fun FormattedBody.toHtmlDocument(
|
||||
permalinkParser: PermalinkParser,
|
||||
prefix: String? = null,
|
||||
): Document? {
|
||||
return takeIf { it.format == MessageFormat.HTML }?.body
|
||||
// Trim whitespace at the end to avoid having wrong rendering of the message.
|
||||
// We don't trim the start in case it's used as indentation.
|
||||
|
|
@ -44,17 +48,20 @@ fun FormattedBody.toHtmlDocument(prefix: String? = null): Document? {
|
|||
}
|
||||
|
||||
// Prepend `@` to mentions
|
||||
fixMentions(dom)
|
||||
fixMentions(dom, permalinkParser)
|
||||
|
||||
dom
|
||||
}
|
||||
}
|
||||
|
||||
private fun fixMentions(dom: Document) {
|
||||
private fun fixMentions(
|
||||
dom: Document,
|
||||
permalinkParser: PermalinkParser,
|
||||
) {
|
||||
val links = dom.getElementsByTag("a")
|
||||
links.forEach {
|
||||
if (it.hasAttr("href")) {
|
||||
val link = PermalinkParser.parse(it.attr("href"))
|
||||
val link = permalinkParser.parse(it.attr("href"))
|
||||
if (link is PermalinkData.UserLink && !it.text().startsWith("@")) {
|
||||
it.prependText("@")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package io.element.android.libraries.matrix.ui.messages
|
||||
|
||||
import io.element.android.libraries.matrix.api.permalink.PermalinkParser
|
||||
import io.element.android.libraries.matrix.api.timeline.item.event.FormattedBody
|
||||
import io.element.android.libraries.matrix.api.timeline.item.event.MessageFormat
|
||||
import io.element.android.libraries.matrix.api.timeline.item.event.TextMessageType
|
||||
|
|
@ -29,15 +30,24 @@ import org.jsoup.select.NodeVisitor
|
|||
* Converts the HTML string in [TextMessageType.formatted] to a plain text representation by parsing it and removing all formatting.
|
||||
* If the message is not formatted or the format is not [MessageFormat.HTML], the [TextMessageType.body] is returned instead.
|
||||
*/
|
||||
fun TextMessageType.toPlainText() = formatted?.toPlainText() ?: body
|
||||
fun TextMessageType.toPlainText(
|
||||
permalinkParser: PermalinkParser,
|
||||
) = formatted?.toPlainText(permalinkParser) ?: body
|
||||
|
||||
/**
|
||||
* Converts the HTML string in [FormattedBody.body] to a plain text representation by parsing it and removing all formatting.
|
||||
* If the message is not formatted or the format is not [MessageFormat.HTML] we return `null`.
|
||||
* @param permalinkParser the parser to use to parse the mentions.
|
||||
* @param prefix if not null, the prefix will be inserted at the beginning of the message.
|
||||
*/
|
||||
fun FormattedBody.toPlainText(prefix: String? = null): String? {
|
||||
return this.toHtmlDocument(prefix)?.toPlainText()
|
||||
fun FormattedBody.toPlainText(
|
||||
permalinkParser: PermalinkParser,
|
||||
prefix: String? = null,
|
||||
): String? {
|
||||
return this.toHtmlDocument(
|
||||
permalinkParser = permalinkParser,
|
||||
prefix = prefix,
|
||||
)?.toPlainText()
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -16,9 +16,13 @@
|
|||
|
||||
package io.element.android.libraries.matrixui.messages
|
||||
|
||||
import android.net.Uri
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.matrix.api.permalink.PermalinkData
|
||||
import io.element.android.libraries.matrix.api.permalink.PermalinkParser
|
||||
import io.element.android.libraries.matrix.api.timeline.item.event.FormattedBody
|
||||
import io.element.android.libraries.matrix.api.timeline.item.event.MessageFormat
|
||||
import io.element.android.libraries.matrix.test.permalink.FakePermalinkParser
|
||||
import io.element.android.libraries.matrix.ui.messages.toHtmlDocument
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
|
@ -33,7 +37,7 @@ class ToHtmlDocumentTest {
|
|||
body = "Hello world"
|
||||
)
|
||||
|
||||
val document = body.toHtmlDocument()
|
||||
val document = body.toHtmlDocument(permalinkParser = FakePermalinkParser())
|
||||
|
||||
assertThat(document).isNull()
|
||||
}
|
||||
|
|
@ -45,7 +49,7 @@ class ToHtmlDocumentTest {
|
|||
body = "<p>Hello world</p>"
|
||||
)
|
||||
|
||||
val document = body.toHtmlDocument()
|
||||
val document = body.toHtmlDocument(permalinkParser = FakePermalinkParser())
|
||||
assertThat(document).isNotNull()
|
||||
assertThat(document?.text()).isEqualTo("Hello world")
|
||||
}
|
||||
|
|
@ -57,7 +61,10 @@ class ToHtmlDocumentTest {
|
|||
body = "<p>Hello world</p>"
|
||||
)
|
||||
|
||||
val document = body.toHtmlDocument(prefix = "@Jorge:")
|
||||
val document = body.toHtmlDocument(
|
||||
permalinkParser = FakePermalinkParser(),
|
||||
prefix = "@Jorge:"
|
||||
)
|
||||
assertThat(document).isNotNull()
|
||||
assertThat(document?.text()).isEqualTo("@Jorge: Hello world")
|
||||
}
|
||||
|
|
@ -69,7 +76,13 @@ class ToHtmlDocumentTest {
|
|||
body = "Hey <a href='https://matrix.to/#/@alice:matrix.org'>Alice</a>!"
|
||||
)
|
||||
|
||||
val document = body.toHtmlDocument()
|
||||
val document = body.toHtmlDocument(permalinkParser = object : PermalinkParser {
|
||||
override fun parse(uriString: String): PermalinkData {
|
||||
return PermalinkData.UserLink("@alice:matrix.org")
|
||||
}
|
||||
|
||||
override fun parse(uri: Uri): PermalinkData = TODO("Not yet implemented")
|
||||
})
|
||||
assertThat(document?.text()).isEqualTo("Hey @Alice!")
|
||||
}
|
||||
|
||||
|
|
@ -80,7 +93,13 @@ class ToHtmlDocumentTest {
|
|||
body = "Hey <a href='https://matrix.to/#/@alice:matrix.org'>@Alice</a>!"
|
||||
)
|
||||
|
||||
val document = body.toHtmlDocument()
|
||||
val document = body.toHtmlDocument(permalinkParser = object : PermalinkParser {
|
||||
override fun parse(uriString: String): PermalinkData {
|
||||
return PermalinkData.UserLink("@alice:matrix.org")
|
||||
}
|
||||
|
||||
override fun parse(uri: Uri): PermalinkData = TODO("Not yet implemented")
|
||||
})
|
||||
assertThat(document?.text()).isEqualTo("Hey @Alice!")
|
||||
}
|
||||
|
||||
|
|
@ -91,7 +110,13 @@ class ToHtmlDocumentTest {
|
|||
body = "Hey <a href='https://matrix.org'>Alice</a>!"
|
||||
)
|
||||
|
||||
val document = body.toHtmlDocument()
|
||||
val document = body.toHtmlDocument(permalinkParser = object : PermalinkParser {
|
||||
override fun parse(uriString: String): PermalinkData {
|
||||
return PermalinkData.FallbackLink(uri = Uri.parse("https://matrix.org"))
|
||||
}
|
||||
|
||||
override fun parse(uri: Uri): PermalinkData = TODO("Not yet implemented")
|
||||
})
|
||||
assertThat(document?.text()).isEqualTo("Hey Alice!")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import com.google.common.truth.Truth.assertThat
|
|||
import io.element.android.libraries.matrix.api.timeline.item.event.FormattedBody
|
||||
import io.element.android.libraries.matrix.api.timeline.item.event.MessageFormat
|
||||
import io.element.android.libraries.matrix.api.timeline.item.event.TextMessageType
|
||||
import io.element.android.libraries.matrix.test.permalink.FakePermalinkParser
|
||||
import io.element.android.libraries.matrix.ui.messages.toPlainText
|
||||
import org.jsoup.Jsoup
|
||||
import org.junit.Test
|
||||
|
|
@ -59,7 +60,7 @@ class ToPlainTextTest {
|
|||
<br />
|
||||
""".trimIndent()
|
||||
)
|
||||
assertThat(formattedBody.toPlainText()).isEqualTo(
|
||||
assertThat(formattedBody.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo(
|
||||
"""
|
||||
Hello world
|
||||
• This is an unordered list.
|
||||
|
|
@ -79,7 +80,7 @@ class ToPlainTextTest {
|
|||
<br />
|
||||
""".trimIndent()
|
||||
)
|
||||
assertThat(formattedBody.toPlainText()).isNull()
|
||||
assertThat(formattedBody.toPlainText(permalinkParser = FakePermalinkParser())).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -96,7 +97,7 @@ class ToPlainTextTest {
|
|||
""".trimIndent()
|
||||
)
|
||||
)
|
||||
assertThat(messageType.toPlainText()).isEqualTo(
|
||||
assertThat(messageType.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo(
|
||||
"""
|
||||
Hello world
|
||||
• This is an unordered list.
|
||||
|
|
@ -119,6 +120,6 @@ class ToPlainTextTest {
|
|||
""".trimIndent()
|
||||
)
|
||||
)
|
||||
assertThat(messageType.toPlainText()).isEqualTo("This is the fallback text")
|
||||
assertThat(messageType.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo("This is the fallback text")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue