diff --git a/src/main/java/sirius/biz/tycho/kb/KnowledgeBaseMessageExpander.java b/src/main/java/sirius/biz/tycho/kb/KnowledgeBaseMessageExpander.java
index bb114e356..c50b1a704 100644
--- a/src/main/java/sirius/biz/tycho/kb/KnowledgeBaseMessageExpander.java
+++ b/src/main/java/sirius/biz/tycho/kb/KnowledgeBaseMessageExpander.java
@@ -14,42 +14,48 @@
import sirius.kernel.nls.NLS;
import sirius.web.controller.MessageExpander;
+import javax.annotation.Nullable;
+import java.util.Optional;
import java.util.regex.Pattern;
/**
- * Expands blocks like kba:ABDCE or [... kba:ABDCE] into proper links to a {@link KnowledgeBaseEntry}.
+ * Expands blocks like kba:ABDCE#section-anchor or [... kba:ABDCE#section-anchor ...] into proper links to a {@link KnowledgeBaseEntry}.
*/
@Register
public class KnowledgeBaseMessageExpander implements MessageExpander {
+ private static final Pattern LOCKED_KBA_PATTERN =
+ Pattern.compile("\\[(.*?)kba:([a-zA-Z0-9]+)(#[a-zA-Z0-9-_]+)?(.*)]");
+ private static final Pattern KBA_PATTERN = Pattern.compile("kba:([a-zA-Z0-9]+)(#[a-zA-Z0-9-_]+)?");
+
+ private static final String EXPANDED_LINK_TEMPLATE = """
+
+ %s
+
+ """;
+
@Part
private KnowledgeBase knowledgeBase;
- private static final Pattern LOCKED_KBA_PATTERN = Pattern.compile("\\[(.*?)kba:([a-zA-Z0-9]+)(.*)]");
- private static final Pattern KBA_PATTERN = Pattern.compile("kba:([a-zA-Z0-9]+)");
-
@Override
public String expand(String message) {
message = LOCKED_KBA_PATTERN.matcher(message).replaceAll(match -> {
- return knowledgeBase.resolve(NLS.getCurrentLanguage(), match.group(2), false).map(kba -> {
- return match.group(1) + Strings.apply("""
-
- %s
-
- """,
- kba.getLanguage(),
- kba.getArticleId(),
- kba.getTitle()) + match.group(3);
- }).orElse("");
+ return knowledgeBase.resolve(NLS.getCurrentLanguage(), match.group(2), false)
+ .map(kba -> match.group(1) + renderTemplate(kba, match.group(3)) + match.group(4))
+ .orElse("");
});
return KBA_PATTERN.matcher(message).replaceAll(match -> {
- return knowledgeBase.resolve(NLS.getCurrentLanguage(), match.group(1), true).map(kba -> {
- return Strings.apply("""
-
- %s
-
- """, kba.getLanguage(), kba.getArticleId(), kba.getTitle());
- }).orElse("kba:" + match.group());
+ return knowledgeBase.resolve(NLS.getCurrentLanguage(), match.group(1), true)
+ .map(kba -> renderTemplate(kba, match.group(2)))
+ .orElse("kba:" + match.group());
});
}
+
+ private static String renderTemplate(KnowledgeBaseArticle kba, @Nullable String anchor) {
+ return Strings.apply(EXPANDED_LINK_TEMPLATE,
+ kba.getLanguage(),
+ kba.getArticleId(),
+ Optional.ofNullable(anchor).orElse(""),
+ kba.getTitle());
+ }
}