Skip to content

Commit 82c8862

Browse files
committed
Add AdventureUrlPostProcessor
1 parent 6237700 commit 82c8862

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.eternalcode.commons.adventure;
2+
3+
import java.util.function.UnaryOperator;
4+
import java.util.regex.Pattern;
5+
import net.kyori.adventure.text.Component;
6+
import net.kyori.adventure.text.TextReplacementConfig;
7+
import net.kyori.adventure.text.event.ClickEvent;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class AdventureUrlPostProcessor implements UnaryOperator<Component> {
11+
12+
private static final Pattern URL_PATTERN = Pattern.compile("https?://(www\\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()!@:%_+.~#?&/=]*)");
13+
14+
public static final @NotNull TextReplacementConfig CLICKABLE_URL_CONFIG = TextReplacementConfig.builder()
15+
.match(URL_PATTERN)
16+
.replacement(url -> url.clickEvent(ClickEvent.openUrl(url.content())))
17+
.build();
18+
19+
@Override
20+
public Component apply(Component component) {
21+
return component.replaceText(CLICKABLE_URL_CONFIG);
22+
}
23+
}

0 commit comments

Comments
 (0)