Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repeating hashtag not highlighting in note content #256

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions app/src/main/kotlin/net/primal/android/core/utils/TextMatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.primal.android.core.utils
class TextMatcher(
content: String,
texts: List<String>,
repeatingOccurrences: Boolean = false,
) {

private val matches: MutableList<TextMatch> = mutableListOf()
Expand All @@ -26,16 +27,21 @@ class TextMatcher(
}

init {
texts.forEach {
val startIndex = content.indexOfNotMatchedBefore(text = it)
if (startIndex != null) {
texts.forEach { text ->
var currentIndex = content.indexOfNotMatchedBefore(text = text)

while (currentIndex != null) {
matches.add(
TextMatch(
value = it,
startIndex = startIndex,
endIndex = startIndex + it.length,
value = text,
startIndex = currentIndex,
endIndex = currentIndex + text.length,
),
)

if (!repeatingOccurrences) break

currentIndex = content.indexOfNotMatchedBefore(text = text)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,11 @@ fun renderContentAsAnnotatedString(
)
}

TextMatcher(content = refinedContent, texts = data.hashtags)
TextMatcher(
content = refinedContent,
texts = data.hashtags,
repeatingOccurrences = true,
)
.matches()
.forEach {
addHashtagAnnotation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.primal.android.core.utils

import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.collections.shouldContainAll
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
import io.kotest.matchers.collections.shouldNotContain
import org.junit.Test

Expand Down Expand Up @@ -31,6 +32,20 @@ class TextMatcherTest {
actual.shouldContainAll(hashtags)
}

@Test
fun `matches should match hashtags and return hashtags with duplicates`() {
val hashtags = listOf("#nostr", "#bitcoin", "#sats", "#freedom")
val expected = listOf("#nostr", "#nostr", "#nostr", "#bitcoin", "#freedom", "#freedom", "#sats")
val matcher = TextMatcher(
content = "Hello I love #nostr #nostr somemore #nostr #bitcoin #freedom #sats #freedom",
texts = hashtags,
repeatingOccurrences = true,
)

val actual = matcher.matches().map { it.value }
actual.shouldContainExactlyInAnyOrder(expected)
}

@Test
fun `matches should not match unknown hashtags`() {
val hashtags = listOf("#Hiking", "#Trails")
Expand Down