mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 14:38:49 +00:00
Port latest URL detection changes from adventure
This commit is contained in:
1 file changed
+9
-1
@@ -20,6 +20,8 @@
|
|||||||
package net.draycia.carbon.common.util;
|
package net.draycia.carbon.common.util;
|
||||||
|
|
||||||
import com.google.common.base.Suppliers;
|
import com.google.common.base.Suppliers;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import net.kyori.adventure.text.TextReplacementConfig;
|
import net.kyori.adventure.text.TextReplacementConfig;
|
||||||
@@ -31,7 +33,7 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
|||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public final class Strings {
|
public final class Strings {
|
||||||
|
|
||||||
private static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
|
private static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/([A-Za-z0-9\\-._~!$&'()*+,;=:@/]|%[0-9A-Fa-f]{2})*)?");
|
||||||
private static final Pattern URL_SCHEME_PATTERN = Pattern.compile("^[a-z][a-z0-9+\\-.]*:");
|
private static final Pattern URL_SCHEME_PATTERN = Pattern.compile("^[a-z][a-z0-9+\\-.]*:");
|
||||||
public static final Supplier<TextReplacementConfig> URL_REPLACEMENT_CONFIG = Suppliers.memoize(
|
public static final Supplier<TextReplacementConfig> URL_REPLACEMENT_CONFIG = Suppliers.memoize(
|
||||||
() -> TextReplacementConfig.builder()
|
() -> TextReplacementConfig.builder()
|
||||||
@@ -41,7 +43,13 @@ public final class Strings {
|
|||||||
if (!URL_SCHEME_PATTERN.matcher(clickUrl).find()) {
|
if (!URL_SCHEME_PATTERN.matcher(clickUrl).find()) {
|
||||||
clickUrl = "http://" + clickUrl;
|
clickUrl = "http://" + clickUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
final URI ignored = new URI(clickUrl); // just to validate that the uri is valid
|
||||||
return url.clickEvent(ClickEvent.openUrl(clickUrl));
|
return url.clickEvent(ClickEvent.openUrl(clickUrl));
|
||||||
|
} catch (final URISyntaxException ignored) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.build()
|
.build()
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in new issue
Block a user