From 5a8a0506c07ba3d0b086835b1b679faecf64d532 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Tue, 16 Apr 2024 19:15:58 +0200 Subject: [PATCH] Fix Bsky facets count emojis and new line --- .../kotlin/fr/shikkanime/wrappers/BskyWrapper.kt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt b/src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt index fb1be431..b15572eb 100644 --- a/src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt +++ b/src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt @@ -8,6 +8,7 @@ import io.ktor.http.* import java.time.ZoneId import java.time.ZonedDateTime import java.time.format.DateTimeFormatter +import java.util.regex.Pattern private const val TYPE = "\$type" @@ -128,10 +129,21 @@ object BskyWrapper { val link = word.trim() if (link.startsWith("http")) { - val beautifulLink = link.replace("https?://www\\.|\\?.*".toRegex(), "") + val beautifulLink = link.replace("https?://www\\.|\\?.*".toRegex(), "").trim() tmpText = tmpText.replace(link, beautifulLink) - val start = tmpText.indexOf(beautifulLink) + // Count \n before the link + val newLineCount = tmpText.substring(0, tmpText.indexOf(beautifulLink)).count { it == '\n' } + // Count the number of emojis before the link + val p = Pattern.compile("\\p{So}+") + val m = p.matcher(tmpText.substring(0, tmpText.indexOf(beautifulLink))) + var emojiCount = 0 + + while (m.find()) { + emojiCount++ + } + + val start = tmpText.indexOf(beautifulLink) + (((newLineCount + emojiCount) * 2) - 1) val end = start + beautifulLink.length Facet(link, start, end) } else null