fix: Incorrect URL regex + extra error handling

This commit is contained in:
Linnea Gräf
2025-06-17 15:59:32 +02:00
parent 589a48a1b3
commit 775933d516

View File

@@ -51,7 +51,7 @@ object ChatLinks : FirmamentFeature {
private fun isUrlAllowed(url: String) = isHostAllowed(url.removePrefix("https://").substringBefore("/")) private fun isUrlAllowed(url: String) = isHostAllowed(url.removePrefix("https://").substringBefore("/"))
override val config get() = TConfig override val config get() = TConfig
val urlRegex = "https://[^. ]+\\.[^ ]+(\\.?( |$))".toRegex() val urlRegex = "https://[^. ]+\\.[^ ]+(\\.?(\\s|$))".toRegex()
val nextTexId = AtomicInteger(0) val nextTexId = AtomicInteger(0)
data class Image( data class Image(
@@ -139,19 +139,20 @@ object ChatLinks : FirmamentFeature {
var index = 0 var index = 0
while (index < text.length) { while (index < text.length) {
val nextMatch = urlRegex.find(text, index) val nextMatch = urlRegex.find(text, index)
if (nextMatch == null) { val url = nextMatch?.groupValues[0]
val uri = runCatching { url?.let(::URI) }.getOrNull()
if (nextMatch == null || url == null) {
s.append(Text.literal(text.substring(index, text.length))) s.append(Text.literal(text.substring(index, text.length)))
break break
} }
val range = nextMatch.groups[0]!!.range val range = nextMatch.groups[0]!!.range
val url = nextMatch.groupValues[0]
s.append(Text.literal(text.substring(index, range.first))) s.append(Text.literal(text.substring(index, range.first)))
s.append( s.append(
Text.literal(url).setStyle( Text.literal(url).setStyle(
Style.EMPTY.withUnderline(true).withColor( Style.EMPTY.withUnderline(true).withColor(
Formatting.AQUA Formatting.AQUA
).withHoverEvent(HoverEvent.ShowText(Text.literal(url))) ).withHoverEvent(HoverEvent.ShowText(Text.literal(url)))
.withClickEvent(ClickEvent.OpenUrl(URI(url))) .withClickEvent(ClickEvent.OpenUrl(uri))
) )
) )
if (isImageUrl(url)) if (isImageUrl(url))