From fc4071fa878103ff669fdb2a007cccb033cfd93e Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Mon, 13 May 2024 21:45:25 -0400 Subject: [PATCH] Links --- app/build.gradle | 1 + .../java/io/heckel/ntfy/ui/DetailAdapter.kt | 10 ++++++-- .../io/heckel/ntfy/util/MarkwonFactory.kt | 24 ++++++++++++------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f610ded..330d2fb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -138,6 +138,7 @@ dependencies { implementation 'io.noties.markwon:core:4.6.2' implementation 'io.noties.markwon:image-picasso:4.6.2' implementation 'io.noties.markwon:image:4.6.2' + implementation 'io.noties.markwon:linkify:4.6.2' implementation 'io.noties.markwon:ext-tables:4.6.2' implementation 'io.noties.markwon:ext-strikethrough:4.6.2' } diff --git a/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt b/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt index a5052a2..0643b88 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt @@ -112,10 +112,16 @@ class DetailAdapter(private val activity: Activity, private val lifecycleScope: val context = itemView.context val unmatchedTags = unmatchedTags(splitTags(notification.tags)) + val message = maybeAppendActionErrors(formatMessage(notification), notification) dateView.text = formatDateShort(notification.timestamp) - messageView.text = maybeAppendActionErrors(maybeMarkdown(formatMessage(notification), notification), notification) - messageView.autoLinkMask = if (notification.isMarkdown()) 0 else Linkify.WEB_URLS + if (notification.isMarkdown()) { + messageView.autoLinkMask = 0 + markwon.setMarkdown(messageView, message.toString()) + } else { + messageView.autoLinkMask = Linkify.WEB_URLS + messageView.text = message + } messageView.movementMethod = BetterLinkMovementMethod.getInstance() messageView.setOnClickListener { // Click & Long-click listeners on the text as well, because "autoLink=web" makes them diff --git a/app/src/main/java/io/heckel/ntfy/util/MarkwonFactory.kt b/app/src/main/java/io/heckel/ntfy/util/MarkwonFactory.kt index f01e674..f747776 100644 --- a/app/src/main/java/io/heckel/ntfy/util/MarkwonFactory.kt +++ b/app/src/main/java/io/heckel/ntfy/util/MarkwonFactory.kt @@ -3,18 +3,18 @@ package io.heckel.ntfy.util import android.content.Context import android.graphics.Color import android.graphics.Typeface -import android.text.method.LinkMovementMethod import android.text.style.* +import android.text.util.Linkify import androidx.core.content.ContextCompat import io.heckel.ntfy.R import io.noties.markwon.* import io.noties.markwon.core.CorePlugin import io.noties.markwon.core.CoreProps import io.noties.markwon.core.MarkwonTheme +import io.noties.markwon.core.spans.LinkSpan import io.noties.markwon.ext.strikethrough.StrikethroughPlugin -import io.noties.markwon.ext.tables.TableAwareMovementMethod -import io.noties.markwon.ext.tables.TablePlugin -import io.noties.markwon.ext.tables.TableTheme +import io.noties.markwon.image.ImagesPlugin +import io.noties.markwon.linkify.LinkifyPlugin import io.noties.markwon.movement.MovementMethodPlugin import me.saket.bettermovementmethod.BetterLinkMovementMethod import org.commonmark.ext.gfm.tables.TableCell @@ -30,14 +30,22 @@ internal object MarkwonFactory { return Markwon.builder(context) .usePlugin(CorePlugin.create()) .usePlugin(MovementMethodPlugin.create(BetterLinkMovementMethod.getInstance())) - // .usePlugin(PicassoImagesPlugin.create(picasso)) + .usePlugin(ImagesPlugin.create()) + .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) .usePlugin(StrikethroughPlugin.create()) - //.usePlugin(TablePlugin.create(context)) .usePlugin(object : AbstractMarkwonPlugin() { override fun configureTheme(builder: MarkwonTheme.Builder) { - builder.linkColor(ContextCompat.getColor(context, R.color.teal)) + builder + .linkColor(ContextCompat.getColor(context, R.color.teal)) .isLinkUnderlined(true) + .blockMargin(0) } + + override fun configureConfiguration(builder: MarkwonConfiguration.Builder) { + builder + .linkResolver(LinkResolverDef()) + } + override fun configureSpansFactory(builder: MarkwonSpansFactory.Builder) { builder .setFactory(Heading::class.java) { _, props: RenderProps? -> @@ -52,7 +60,7 @@ internal object MarkwonFactory { .setFactory(Code::class.java) { _, _ -> arrayOf( BackgroundColorSpan(Color.LTGRAY), - TypefaceSpan("monospace") + //TypefaceSpan("monospace") ) } .setFactory(ListItem::class.java) { _, _ -> BulletSpan(bulletGapWidth) }