Skip to content

Commit

Permalink
Improve url link recognition (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmedalijaK authored Feb 20, 2025
1 parent 0af168a commit 88176a5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fun String.parseUris(includeNostrUris: Boolean = true): List<String> {
.filterInvalidTLDs()
.map { it.originalUrl }
val customUrls = this.detectUrls()
val mergedUrls = mergeUrls(emptyList(), customUrls)
val mergedUrls = mergeUrls(libUrls, customUrls)

return if (includeNostrUris) {
val nostr = this.parseNostrUris()
Expand Down
58 changes: 58 additions & 0 deletions app/src/test/kotlin/net/primal/android/core/utils/UriUtilsTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.primal.android.core.utils

import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.ints.shouldBeExactly
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
Expand Down Expand Up @@ -37,6 +38,63 @@ class UriUtilsTest {
expectedUrls.size shouldBeExactly 1
}

@Test
fun `parseUrls should recognize various url formats`() {
val content = """
Some random links about bitcoin:
https://en.m.wikipedia.org/wiki/Bit_(money)
Check out primal.net for more info!
Visit us at www.primal.net or at https://primal.net
Here's a link to a secure site: https://www.example.com/path/to/resource
And a simple link: example.com
Don't forget the test with brackets: https://example.com/page?(query)=1&sort=desc
""".trimIndent()

val expectedUrls = content.parseUris()

expectedUrls shouldBe listOf(
"primal.net",
"www.primal.net",
"https://primal.net",
"https://www.example.com/path/to/resource",
"example.com",
"https://en.m.wikipedia.org/wiki/Bit_(money)",
"https://example.com/page?(query)=1&sort=desc",
)
expectedUrls.size shouldBeExactly 7
}

@Test
fun `parseUrls should recognize urls with port numbers`() {
val content = """
A link with a port number:
https://www.example.com:443/resource
""".trimIndent()

val expectedUrls = content.parseUris()

expectedUrls.shouldNotBeNull()
expectedUrls shouldContainExactly listOf(
"https://www.example.com:443/resource"
)
}

@Test
fun `parseUrls should return empty for invalid urls`() {
val content = """
Some random links:
thisisnotalink
http://
www.
example@com
""".trimIndent()

val expectedUrls = content.parseUris()

expectedUrls.shouldNotBeNull()
expectedUrls.size shouldBe 0
}

@Test
fun `parseUrls should not return urls with brackets`() {
val hugeContent = javaClass.getResource("/core/release_notes.txt")?.readText()
Expand Down

0 comments on commit 88176a5

Please sign in to comment.