diff options
| author | Linnea Gräf <nea@nea.moe> | 2025-06-17 15:59:32 +0200 |
|---|---|---|
| committer | Linnea Gräf <nea@nea.moe> | 2025-06-17 20:59:54 +0200 |
| commit | 775933d516db10dbcc1cbbe405defa7b6e0c5a6a (patch) | |
| tree | 5970eabf04b78b559c5c125d3fc7394f18c6c2db /src | |
| parent | 589a48a1b301cc9d5a651c22aea3f0814fd54144 (diff) | |
| download | Firmament-775933d516db10dbcc1cbbe405defa7b6e0c5a6a.tar.gz Firmament-775933d516db10dbcc1cbbe405defa7b6e0c5a6a.tar.bz2 Firmament-775933d516db10dbcc1cbbe405defa7b6e0c5a6a.zip | |
fix: Incorrect URL regex + extra error handling
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/kotlin/features/chat/ChatLinks.kt | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/src/main/kotlin/features/chat/ChatLinks.kt b/src/main/kotlin/features/chat/ChatLinks.kt index a084234..869fe62 100644 --- a/src/main/kotlin/features/chat/ChatLinks.kt +++ b/src/main/kotlin/features/chat/ChatLinks.kt @@ -51,7 +51,7 @@ object ChatLinks : FirmamentFeature { private fun isUrlAllowed(url: String) = isHostAllowed(url.removePrefix("https://").substringBefore("/")) override val config get() = TConfig - val urlRegex = "https://[^. ]+\\.[^ ]+(\\.?( |$))".toRegex() + val urlRegex = "https://[^. ]+\\.[^ ]+(\\.?(\\s|$))".toRegex() val nextTexId = AtomicInteger(0) data class Image( @@ -139,19 +139,20 @@ object ChatLinks : FirmamentFeature { var index = 0 while (index < text.length) { 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))) break } val range = nextMatch.groups[0]!!.range - val url = nextMatch.groupValues[0] s.append(Text.literal(text.substring(index, range.first))) s.append( Text.literal(url).setStyle( Style.EMPTY.withUnderline(true).withColor( Formatting.AQUA ).withHoverEvent(HoverEvent.ShowText(Text.literal(url))) - .withClickEvent(ClickEvent.OpenUrl(URI(url))) + .withClickEvent(ClickEvent.OpenUrl(uri)) ) ) if (isImageUrl(url)) |
