diff --git a/src/main/java/run/halo/injection/AbstractHtmlProcessor.java b/src/main/java/run/halo/injection/AbstractHtmlProcessor.java index 73a1814c..1f3d1179 100644 --- a/src/main/java/run/halo/injection/AbstractHtmlProcessor.java +++ b/src/main/java/run/halo/injection/AbstractHtmlProcessor.java @@ -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 pageRules) { - return true; + // 匹配路径 + protected boolean isRequestPathMatchingRoute(ITemplateContext context, Set 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; } } diff --git a/src/main/java/run/halo/injection/HtmlFooterProcessor.java b/src/main/java/run/halo/injection/HtmlFooterProcessor.java index b21fac45..066a3b81 100644 --- a/src/main/java/run/halo/injection/HtmlFooterProcessor.java +++ b/src/main/java/run/halo/injection/HtmlFooterProcessor.java @@ -21,7 +21,7 @@ public Mono 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())); diff --git a/src/main/java/run/halo/injection/HtmlHeadProcessor.java b/src/main/java/run/halo/injection/HtmlHeadProcessor.java index 49d2252f..854b5bb6 100644 --- a/src/main/java/run/halo/injection/HtmlHeadProcessor.java +++ b/src/main/java/run/halo/injection/HtmlHeadProcessor.java @@ -20,7 +20,7 @@ public Mono 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())); diff --git a/ui/src/views/HtmlInjectionAdd.vue b/ui/src/views/HtmlInjectionAdd.vue index cbbf35c9..5462871f 100644 --- a/ui/src/views/HtmlInjectionAdd.vue +++ b/ui/src/views/HtmlInjectionAdd.vue @@ -125,9 +125,9 @@ const submitForm = () => { { value: 'FOOTER', label: 'Footer' } ]" /> - +