Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance KBA message expander to support linking to section via anchor #1951

Merged
merged 5 commits into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions src/main/java/sirius/biz/tycho/kb/KnowledgeBaseMessageExpander.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>kba:ABDCE</tt> or <tt>[... kba:ABDCE]</tt> into proper links to a {@link KnowledgeBaseEntry}.
* Expands blocks like <tt>kba:ABDCE#section-anchor</tt> or <tt>[... kba:ABDCE#section-anchor ...]</tt> 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 = """
<span class="d-inline-flex flex-row align-items-baseline">
<i class="fa-solid fa-lightbulb"></i><a class="ps-1" href="/kba/%s/%s%s">%s</a>
</span>
""";

@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("""
<span class="d-inline-flex flex-row align-items-baseline">
<i class="fa-solid fa-lightbulb"></i><a class="ps-1" href="/kba/%s/%s">%s</a>
</span>
""",
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("""
<span class="d-inline-flex flex-row align-items-baseline">
<i class="fa-solid fa-lightbulb"></i><a class="ps-1" href="/kba/%s/%s">%s</a>
</span>
""", 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());
}
}