-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
TemplateGlobalHeadProcessor.java
59 lines (51 loc) · 2.24 KB
/
TemplateGlobalHeadProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package run.halo.app.theme.dialect;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.processor.element.IElementModelStructureHandler;
import reactor.core.publisher.Mono;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.theme.DefaultTemplateEnum;
import run.halo.app.theme.router.ModelConst;
/**
* <p>Global custom head snippet injection for theme global setting.</p>
*
* @author guqing
* @since 2.0.0
*/
@Component
public class TemplateGlobalHeadProcessor implements TemplateHeadProcessor {
private final SystemConfigurableEnvironmentFetcher fetcher;
public TemplateGlobalHeadProcessor(SystemConfigurableEnvironmentFetcher fetcher) {
this.fetcher = fetcher;
}
@Override
public Mono<Void> process(ITemplateContext context, IModel model,
IElementModelStructureHandler structureHandler) {
final IModelFactory modelFactory = context.getModelFactory();
return fetchCodeInjection()
.doOnNext(codeInjection -> {
String globalHeader = codeInjection.getGlobalHead();
if (StringUtils.isNotBlank(globalHeader)) {
model.add(modelFactory.createText(globalHeader + "\n"));
}
// add content head to model
String contentHeader = codeInjection.getContentHead();
if (StringUtils.isNotBlank(contentHeader) && isContentTemplate(context)) {
model.add(modelFactory.createText(contentHeader + "\n"));
}
})
.then();
}
private Mono<SystemSetting.CodeInjection> fetchCodeInjection() {
return fetcher.fetch(SystemSetting.CodeInjection.GROUP, SystemSetting.CodeInjection.class);
}
private boolean isContentTemplate(ITemplateContext context) {
String templateId = (String) context.getVariable(ModelConst.TEMPLATE_ID);
return DefaultTemplateEnum.POST.getValue().equals(templateId)
|| DefaultTemplateEnum.SINGLE_PAGE.getValue().equals(templateId);
}
}