From cb6401c7abc636da0fef8c1ce19dc0cecc61fbc3 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 17 Mar 2023 18:01:08 -0400 Subject: [PATCH] Don't preview urls without schema. Show inline link instead. --- .../amethyst/ui/components/RichTextViewer.kt | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt index a811c8b2e..ee185d6d9 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt @@ -48,7 +48,10 @@ import java.util.regex.Pattern val imageExtension = Pattern.compile("(.*/)*.+\\.(png|jpg|gif|bmp|jpeg|webp|svg)$", Pattern.CASE_INSENSITIVE) val videoExtension = Pattern.compile("(.*/)*.+\\.(mp4|avi|wmv|mpg|amv|webm|mov)$", Pattern.CASE_INSENSITIVE) -val noProtocolUrlValidator = Pattern.compile("^[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&//=]*)$") + +// Group 1 = url, group 4 additional chars +val noProtocolUrlValidator = Pattern.compile("(([\\w\\d-]+\\.)*[a-zA-Z][\\w-]+[\\.\\:]\\w+([\\/\\?\\=\\&\\#\\.]?[\\w-]+)*\\/?)(.*)") + val tagIndex = Pattern.compile(".*\\#\\[([0-9]+)\\].*") val mentionsPattern: Pattern = Pattern.compile("@([A-Za-z0-9_\\-]+)") @@ -146,7 +149,13 @@ fun RichTextViewer( } else if (Patterns.PHONE.matcher(word).matches() && word.length > 6) { ClickablePhone(word) } else if (noProtocolUrlValidator.matcher(word).matches()) { - UrlPreview("https://$word", word) + val matcher = noProtocolUrlValidator.matcher(word) + matcher.find() + val url = matcher.group(1) // url + val additionalChars = matcher.group(4) ?: "" // additional chars + + ClickableUrl(url, "https://$url") + Text("$additionalChars") } else if (tagIndex.matcher(word).matches() && tags != null) { TagLink(word, tags, canPreview, backgroundColor, accountViewModel, navController) } else if (hashTagsPattern.matcher(word).matches()) { @@ -167,7 +176,13 @@ fun RichTextViewer( } else if (Patterns.PHONE.matcher(word).matches() && word.length > 6) { ClickablePhone(word) } else if (noProtocolUrlValidator.matcher(word).matches()) { - ClickableUrl(word, "https://$word") + val matcher = noProtocolUrlValidator.matcher(word) + matcher.find() + val url = matcher.group(1) // url + val additionalChars = matcher.group(4) ?: "" // additional chars + + ClickableUrl(url, "https://$url") + Text("$additionalChars") } else if (tagIndex.matcher(word).matches() && tags != null) { TagLink(word, tags, canPreview, backgroundColor, accountViewModel, navController) } else if (hashTagsPattern.matcher(word).matches()) {