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

Implement path matching logic for HTML injection #11

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
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
42 changes: 35 additions & 7 deletions src/main/java/run/halo/injection/AbstractHtmlProcessor.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
package run.halo.injection;

import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.PathContainer;
import org.springframework.util.RouteMatcher;
import org.springframework.web.util.pattern.PathPatternParser;
import org.springframework.web.util.pattern.PathPatternRouteMatcher;
import org.springframework.web.util.pattern.PatternParseException;
import org.thymeleaf.context.Contexts;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.web.IWebRequest;

@Slf4j
public abstract class AbstractHtmlProcessor {
protected static final String TEMPLATE_ID_VARIABLE = "_templateId";
private final RouteMatcher routeMatcher = createRouteMatcher();

protected boolean isContentTemplate(ITemplateContext context) {
return "post".equals(context.getVariable(TEMPLATE_ID_VARIABLE))
|| "page".equals(context.getVariable(TEMPLATE_ID_VARIABLE));
private RouteMatcher createRouteMatcher() {
PathPatternParser parser = new PathPatternParser();
parser.setPathOptions(PathContainer.Options.HTTP_PATH);
return new PathPatternRouteMatcher(parser);
}

// 匹配路径接口
protected boolean isRequestPathMatchingRoute(String requestRoute, Set<String> pageRules) {
return true;
// 匹配路径
protected boolean isRequestPathMatchingRoute(ITemplateContext context, Set<String> pageRules) {
if (!Contexts.isWebContext(context)) {
return false;
}
IWebRequest request = Contexts.asWebContext(context).getExchange().getRequest();
String requestPath = request.getRequestPath();
RouteMatcher.Route requestRoute = routeMatcher.parseRoute(requestPath);

// 遍历 pageRules 中的路径模式,检查是否有匹配的
for (String pathPattern : pageRules) {
try {
if (routeMatcher.match(pathPattern, requestRoute)) {
return true;
}
} catch (PatternParseException e) {
// ignore
log.warn("Parse route pattern [{}] failed", pathPattern, e);
}
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/main/java/run/halo/injection/HtmlFooterProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Mono<Void> process(ITemplateContext context, IProcessableElementTag tag,
IElementTagStructureHandler structureHandler, IModel model) {
return htmlService.listEnabledInjectionsByPoint(HtmlInjection.InjectionPoint.FOOTER)
.doOnNext(htmlInjection -> {
if (isContentTemplate(context)) {
if (isRequestPathMatchingRoute(context, htmlInjection.getSpec().getPageRules())) {
model.add(
context.getModelFactory().createText(
htmlInjection.getSpec().getFragment()));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/injection/HtmlHeadProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Mono<Void> process(ITemplateContext context, IModel model,
IElementModelStructureHandler structureHandler) {
return htmlService.listEnabledInjectionsByPoint(HtmlInjection.InjectionPoint.HEADER)
.doOnNext(htmlInjection -> {
if (isContentTemplate(context)) {
if (isRequestPathMatchingRoute(context, htmlInjection.getSpec().getPageRules())) {
model.add(
context.getModelFactory().createText(
htmlInjection.getSpec().getFragment()));
Expand Down
4 changes: 2 additions & 2 deletions ui/src/views/HtmlInjectionAdd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ const submitForm = () => {
{ value: 'FOOTER', label: 'Footer' }
]"
/>
<FormKit id="pageRules" name="pageRules" :label="'页面匹配规则'" type="list" item-type="string" add-label="添加">
<FormKit id="pageRules" name="pageRules" :label="'页面匹配规则'" validation="required" type="list" item-type="string" add-label="添加">
<template #default="{ index }">
<FormKit type="text" :index="index" help="用于匹配页面路径的正则表达式,如:/archives/**"/>
<FormKit type="text" :index="index" help="用于匹配页面路径的符合 Ant-style 的表达式,如:/archives/**"/>
</template>
</FormKit>
<FormKit id="isEnabled" name="isEnabled" :label="'启用'" type="checkbox"/>
Expand Down