Skip to content

Commit

Permalink
Merge pull request #379 from Shikkanime/dev
Browse files Browse the repository at this point in the history
Fix Bsky facets count emojis and new line
  • Loading branch information
Ziedelth committed Apr 16, 2024
2 parents bbf4b0c + 5a8a050 commit e084061
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e084061

Please sign in to comment.