Html : introduce markown lib to handle html rendering.

This commit is contained in:
ganfra 2019-02-21 19:21:08 +01:00
parent 14ac3a8ae6
commit 4458e28ce2
6 changed files with 85 additions and 4 deletions

View file

@ -60,6 +60,7 @@ dependencies {
def epoxy_version = "3.0.0" def epoxy_version = "3.0.0"
def arrow_version = "0.8.2" def arrow_version = "0.8.2"
def markwon_version = '3.0.0-SNAPSHOT'
implementation project(":matrix-sdk-android") implementation project(":matrix-sdk-android")
implementation project(":matrix-sdk-android-rx") implementation project(":matrix-sdk-android-rx")
@ -97,6 +98,9 @@ dependencies {
implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
implementation 'com.google.android.material:material:1.1.0-alpha02' implementation 'com.google.android.material:material:1.1.0-alpha02'
implementation 'me.gujun.android:span:1.7' implementation 'me.gujun.android:span:1.7'
implementation "ru.noties.markwon:core:$markwon_version"
implementation "ru.noties.markwon:html:$markwon_version"
// DI // DI
implementation "org.koin:koin-android:$koin_version" implementation "org.koin:koin-android:$koin_version"

View file

@ -22,6 +22,7 @@ import im.vector.riotredesign.core.resources.ColorProvider
import im.vector.riotredesign.core.resources.LocaleProvider import im.vector.riotredesign.core.resources.LocaleProvider
import im.vector.riotredesign.core.resources.StringProvider import im.vector.riotredesign.core.resources.StringProvider
import im.vector.riotredesign.features.home.room.list.RoomSelectionRepository import im.vector.riotredesign.features.home.room.list.RoomSelectionRepository
import im.vector.riotredesign.features.markdown.HtmlRenderer
import org.koin.dsl.module.module import org.koin.dsl.module.module
class AppModule(private val context: Context) { class AppModule(private val context: Context) {
@ -48,5 +49,9 @@ class AppModule(private val context: Context) {
RoomSelectionRepository(get()) RoomSelectionRepository(get())
} }
single {
HtmlRenderer(context)
}
} }
} }

View file

@ -33,7 +33,7 @@ class HomeModule {
} }
single { single {
MessageItemFactory(get(), get(), get()) MessageItemFactory(get(), get(), get(), get())
} }
single { single {

View file

@ -29,12 +29,14 @@ import im.vector.riotredesign.core.epoxy.RiotEpoxyModel
import im.vector.riotredesign.core.extensions.localDateTime import im.vector.riotredesign.core.extensions.localDateTime
import im.vector.riotredesign.core.resources.ColorProvider import im.vector.riotredesign.core.resources.ColorProvider
import im.vector.riotredesign.features.home.room.detail.timeline.helper.TimelineMediaSizeProvider import im.vector.riotredesign.features.home.room.detail.timeline.helper.TimelineMediaSizeProvider
import im.vector.riotredesign.features.markdown.HtmlRenderer
import im.vector.riotredesign.features.media.MediaContentRenderer import im.vector.riotredesign.features.media.MediaContentRenderer
import me.gujun.android.span.span import me.gujun.android.span.span
class MessageItemFactory(private val colorProvider: ColorProvider, class MessageItemFactory(private val colorProvider: ColorProvider,
private val timelineMediaSizeProvider: TimelineMediaSizeProvider, private val timelineMediaSizeProvider: TimelineMediaSizeProvider,
private val timelineDateFormatter: TimelineDateFormatter) { private val timelineDateFormatter: TimelineDateFormatter,
private val htmlRenderer: HtmlRenderer) {
private val messagesDisplayedWithInformation = HashSet<String?>() private val messagesDisplayedWithInformation = HashSet<String?>()
@ -102,9 +104,15 @@ class MessageItemFactory(private val colorProvider: ColorProvider,
informationData: MessageInformationData, informationData: MessageInformationData,
callback: TimelineEventController.Callback?): MessageTextItem? { callback: TimelineEventController.Callback?): MessageTextItem? {
val message = linkifyBody(messageContent.body, callback) val bodyToUse = messageContent.formattedBody
?.let {
htmlRenderer.render(it)
}
?: messageContent.body
val linkifiedBody = linkifyBody(bodyToUse, callback)
return MessageTextItem_() return MessageTextItem_()
.message(message) .message(linkifiedBody)
.informationData(informationData) .informationData(informationData)
} }

View file

@ -0,0 +1,63 @@
/*
*
* * Copyright 2019 New Vector Ltd
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package im.vector.riotredesign.features.markdown
import android.content.Context
import ru.noties.markwon.AbstractMarkwonPlugin
import ru.noties.markwon.Markwon
import ru.noties.markwon.MarkwonVisitor
import ru.noties.markwon.html.HtmlPlugin
import ru.noties.markwon.html.HtmlTag
import ru.noties.markwon.html.MarkwonHtmlRenderer
import ru.noties.markwon.html.TagHandler
import timber.log.Timber
class HtmlRenderer(private val context: Context) {
private val markwon = Markwon.builder(context)
.usePlugin(HtmlPlugin.create())
.usePlugin(MatrixPlugin.create())
.build()
fun render(text: String): CharSequence {
return markwon.toMarkdown(text)
}
}
private class MatrixPlugin private constructor() : AbstractMarkwonPlugin() {
override fun configureHtmlRenderer(builder: MarkwonHtmlRenderer.Builder) {
builder.addHandler("mx-reply", MxReplyTagHandler())
}
companion object {
fun create(): MatrixPlugin {
return MatrixPlugin()
}
}
}
private class MxReplyTagHandler : TagHandler() {
override fun handle(visitor: MarkwonVisitor, renderer: MarkwonHtmlRenderer, tag: HtmlTag) {
Timber.v("Handle mx-reply")
}
}

View file

@ -22,6 +22,7 @@ allprojects {
google() google()
jcenter() jcenter()
maven { url 'https://jitpack.io' } maven { url 'https://jitpack.io' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
} }
} }