From 3ec7e31cacab6bd63908df35309caa86d7cbd0d5 Mon Sep 17 00:00:00 2001
From: guqing <38999863+guqing@users.noreply.github.com>
Date: Wed, 26 Oct 2022 11:12:10 +0800
Subject: [PATCH] refactor: fill in the name of the rendered template as the
template id to the view model (#2626)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
#### What type of PR is this?
/kind improvement
/area core
/milestone 2.0
#### What this PR does / why we need it:
渲染模板页面时将被渲染的模板名称填充到试图模型中作为 templateId
为什么需要它:
1. thymeleaf 渲染模板可以使用 fragment,此时在 thymeleaf 的 IElementTagProcessor 等处理器中获取到的 template name不一定是例如 post.html这样的名称
2. 比如渲染文章模板 post.html 而 #2569 计划将要在 theme.yaml 中通过配置允许用户选择文章所渲染的模板,因为无法确定渲染模板的标志,例如有些处理器想在文章页插入 head 这样的需求就需要知道哪个模板是文章页。see also [issuecomment-1215135195](https://github.com/halo-dev/halo/issues/2322#issuecomment-1215135195)
所以目前想到的是通过在 render 时填充一个 templateId 到 view model 中标识模板身份
#### Which issue(s) this PR fixes:
Fixes #2621
#### Special notes for your reviewer:
how to test it?
见 issue #2621
/cc @halo-dev/sig-halo
#### Does this PR introduce a user-facing change?
```release-note
修复设置了文章的 htmlMetas 字段但没有在页面的 head 注入标签的问题
```
---
.../dialect/PostTemplateHeadProcessor.java | 13 ++++++++-----
.../dialect/TemplateGlobalHeadProcessor.java | 11 ++++++-----
.../strategy/CategoriesRouteStrategy.java | 3 ++-
.../router/strategy/CategoryRouteStrategy.java | 3 ++-
.../router/strategy/IndexRouteStrategy.java | 3 ++-
.../app/theme/router/strategy/ModelConst.java | 12 ++++++++++++
.../router/strategy/PermalinkPredicates.java | 17 -----------------
.../router/strategy/PostRouteStrategy.java | 4 ++++
.../strategy/SinglePageRouteStrategy.java | 5 ++++-
.../theme/router/strategy/TagRouteStrategy.java | 4 +++-
.../router/strategy/TagsRouteStrategy.java | 5 ++++-
.../theme/dialect/HaloProcessorDialectTest.java | 3 +++
12 files changed, 50 insertions(+), 33 deletions(-)
create mode 100644 src/main/java/run/halo/app/theme/router/strategy/ModelConst.java
delete mode 100644 src/main/java/run/halo/app/theme/router/strategy/PermalinkPredicates.java
diff --git a/src/main/java/run/halo/app/theme/dialect/PostTemplateHeadProcessor.java b/src/main/java/run/halo/app/theme/dialect/PostTemplateHeadProcessor.java
index 35a2fc985..ba1a839db 100644
--- a/src/main/java/run/halo/app/theme/dialect/PostTemplateHeadProcessor.java
+++ b/src/main/java/run/halo/app/theme/dialect/PostTemplateHeadProcessor.java
@@ -11,6 +11,7 @@ import org.thymeleaf.processor.element.IElementModelStructureHandler;
import reactor.core.publisher.Mono;
import run.halo.app.theme.DefaultTemplateEnum;
import run.halo.app.theme.finders.PostFinder;
+import run.halo.app.theme.router.strategy.ModelConst;
/**
*
The head
html snippet injection processor for post template.
@@ -31,9 +32,10 @@ public class PostTemplateHeadProcessor implements TemplateHeadProcessor {
@Override
public Mono process(ITemplateContext context, IModel model,
IElementModelStructureHandler structureHandler) {
- return Mono.just(context.getTemplateData().getTemplate())
- .filter(this::isPostTemplate)
- .map(template -> (String) context.getVariable(POST_NAME_VARIABLE))
+ if (!isPostTemplate(context)) {
+ return Mono.empty();
+ }
+ return Mono.justOrEmpty((String) context.getVariable(POST_NAME_VARIABLE))
.map(postFinder::getByName)
.doOnNext(postVo -> {
List