Skip to content

Commit

Permalink
Implement path matching logic for HTML injection (halo-sigs#11)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind feature
#### What this PR does / why we need it:
此 PR 增加路径匹配逻辑,用户访问特定的页面时,检测到匹配该页面路径的所有启用的代码片段,完成注入
#### Which issue(s) this PR fixes:
Fixes halo-sigs#10 
#### Does this PR introduce a user-facing change?
```release-note
None
```
  • Loading branch information
CaiHaosen committed Aug 22, 2024
1 parent af78d87 commit 5f73e2d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
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

0 comments on commit 5f73e2d

Please sign in to comment.