Using the URL Detector (which is faster) to catch urls in the text

This commit is contained in:
Vitor Pamplona
2023-04-20 10:08:32 -04:00
parent 64321dbe7c
commit 42801e3eca

View File

@@ -32,6 +32,8 @@ import com.halilibo.richtext.markdown.MarkdownParseOptions
import com.halilibo.richtext.ui.RichTextStyle import com.halilibo.richtext.ui.RichTextStyle
import com.halilibo.richtext.ui.material.MaterialRichText import com.halilibo.richtext.ui.material.MaterialRichText
import com.halilibo.richtext.ui.resolveDefaults import com.halilibo.richtext.ui.resolveDefaults
import com.linkedin.urls.detection.UrlDetector
import com.linkedin.urls.detection.UrlDetectorOptions
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
@@ -132,22 +134,13 @@ fun RichTextViewer(
) )
} }
} else { } else {
val imagesForPager = mutableListOf<String>() val urls = UrlDetector(content, UrlDetectorOptions.Default).detect()
val urlSet = urls.mapTo(LinkedHashSet(urls.size)) { it.originalUrl }
content.split('\n').forEach { paragraph -> val imagesForPager = urlSet.filter { fullUrl ->
paragraph.split(' ').forEach { word: String -> val removedParamsFromUrl = fullUrl.split("?")[0].lowercase()
// sequence of images will render in a slideview imageExtensions.any { removedParamsFromUrl.endsWith(it) } || videoExtensions.any { removedParamsFromUrl.endsWith(it) }
if (isValidURL(word)) {
val removedParamsFromUrl = word.split("?")[0].lowercase()
if (imageExtensions.any { removedParamsFromUrl.endsWith(it) }) {
imagesForPager.add(word)
}
if (videoExtensions.any { removedParamsFromUrl.endsWith(it) }) {
imagesForPager.add(word)
}
}
}
} }
val imagesForPagerSet = imagesForPager.toSet()
// FlowRow doesn't work well with paragraphs. So we need to split them // FlowRow doesn't work well with paragraphs. So we need to split them
content.split('\n').forEach { paragraph -> content.split('\n').forEach { paragraph ->
@@ -156,15 +149,10 @@ fun RichTextViewer(
s.forEach { word: String -> s.forEach { word: String ->
if (canPreview) { if (canPreview) {
// Explicit URL // Explicit URL
if (isValidURL(word)) { if (imagesForPagerSet.contains(word)) {
val removedParamsFromUrl = word.split("?")[0].lowercase() ZoomableImageView(word, imagesForPager)
if (imageExtensions.any { removedParamsFromUrl.endsWith(it) }) { } else if (urlSet.contains(word)) {
ZoomableImageView(word, imagesForPager) UrlPreview(word, "$word ")
} else if (videoExtensions.any { removedParamsFromUrl.endsWith(it) }) {
ZoomableImageView(word, imagesForPager)
} else {
UrlPreview(word, "$word ")
}
} else if (word.startsWith("lnbc", true)) { } else if (word.startsWith("lnbc", true)) {
MayBeInvoicePreview(word) MayBeInvoicePreview(word)
} else if (word.startsWith("lnurl", true)) { } else if (word.startsWith("lnurl", true)) {
@@ -214,7 +202,7 @@ fun RichTextViewer(
) )
} }
} else { } else {
if (isValidURL(word)) { if (urlSet.contains(word)) {
ClickableUrl("$word ", word) ClickableUrl("$word ", word)
} else if (word.startsWith("lnurl", true)) { } else if (word.startsWith("lnurl", true)) {
val lnWithdrawal = LnWithdrawalUtil.findWithdrawal(word) val lnWithdrawal = LnWithdrawalUtil.findWithdrawal(word)