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:
Benoit Marty 2024-04-02 12:35:35 +02:00 committed by Benoit Marty
parent 5ee1b8f79b
commit db3f17fd7d
52 changed files with 771 additions and 318 deletions

View file

@ -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("@")
}

View file

@ -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()
}
/**