-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:implement global HTML injection for head and footer
- Loading branch information
CaiHaosen
committed
Aug 7, 2024
1 parent
864f49b
commit f36ae3f
Showing
6 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/run/halo/injection/AbstractHtmlProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package run.halo.injection; | ||
|
||
import java.util.Set; | ||
import org.thymeleaf.context.ITemplateContext; | ||
|
||
public abstract class AbstractHtmlProcessor { | ||
|
||
protected static final String TEMPLATE_ID_VARIABLE = "_templateId"; | ||
|
||
protected boolean isContentTemplate(ITemplateContext context) { | ||
return "post".equals(context.getVariable(TEMPLATE_ID_VARIABLE)) | ||
|| "page".equals(context.getVariable(TEMPLATE_ID_VARIABLE)); | ||
} | ||
|
||
// 匹配路径接口 | ||
protected boolean isRequestPathMatchingRoute(String requestRoute, Set<String> pageRules) { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package run.halo.injection; | ||
|
||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.thymeleaf.context.ITemplateContext; | ||
import org.thymeleaf.model.IModel; | ||
import org.thymeleaf.model.IProcessableElementTag; | ||
import org.thymeleaf.processor.element.IElementTagStructureHandler; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.theme.dialect.TemplateFooterProcessor; | ||
|
||
|
||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class HtmlFooterProcessor extends AbstractHtmlProcessor implements TemplateFooterProcessor { | ||
|
||
private final HtmlService htmlService; | ||
|
||
@Override | ||
public Mono<Void> process(ITemplateContext context, IProcessableElementTag tag, | ||
IElementTagStructureHandler structureHandler, IModel model) { | ||
return htmlService.getFooterEnabledInjections() | ||
.doOnNext(htmlInjection -> { | ||
if (htmlInjection != null && isContentTemplate(context)) { | ||
model.add( | ||
context.getModelFactory().createText( | ||
htmlInjection.getSpec().getFragment())); | ||
} | ||
}) | ||
.onErrorResume(e -> { | ||
log.error("HtmlFooterProcessor process failed", e); | ||
return Mono.empty(); | ||
}) | ||
.then(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package run.halo.injection; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.thymeleaf.context.ITemplateContext; | ||
import org.thymeleaf.model.IModel; | ||
import org.thymeleaf.processor.element.IElementModelStructureHandler; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.theme.dialect.TemplateHeadProcessor; | ||
|
||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class HtmlHeadProcessor extends AbstractHtmlProcessor implements TemplateHeadProcessor { | ||
|
||
private final HtmlService htmlService; | ||
|
||
@Override | ||
public Mono<Void> process(ITemplateContext context, IModel model, | ||
IElementModelStructureHandler structureHandler) { | ||
return htmlService.getHeadEnabledInjections() | ||
.doOnNext(htmlInjection -> { | ||
if (htmlInjection != null && isContentTemplate(context)) { | ||
model.add( | ||
context.getModelFactory().createText( | ||
htmlInjection.getSpec().getFragment())); | ||
} | ||
}) | ||
.onErrorResume(e -> { | ||
log.error("HtmlHeadProcessor process failed", e); | ||
return Mono.empty(); | ||
}) | ||
.then(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package run.halo.injection; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Flux; | ||
import run.halo.app.extension.ListOptions; | ||
import run.halo.app.extension.ReactiveExtensionClient; | ||
import run.halo.app.extension.index.query.QueryFactory; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class HtmlService { | ||
private final ReactiveExtensionClient client; | ||
|
||
/** | ||
* 获取所有启用的 HtmlInjection 对象. | ||
*/ | ||
public Flux<HtmlInjection> getAllEnabledInjections() { | ||
ListOptions options = ListOptions.builder() | ||
// 在这里添加查询条件,只获取正在启用的代码 | ||
.fieldQuery(QueryFactory.equal("spec.enabled", "true")) | ||
.build(); | ||
return client.listAll(HtmlInjection.class, options, | ||
Sort.by(Sort.Order.asc("metadata.name"))); | ||
} | ||
|
||
/** | ||
* 获取启用的注入点为 HEAD 的 HtmlInjection 对象. | ||
*/ | ||
public Flux<HtmlInjection> getHeadEnabledInjections() { | ||
return getAllEnabledInjections() | ||
.filter(htmlInjection -> | ||
htmlInjection.getSpec().getInjectionPoint() == HtmlInjection.InjectionPoint.HEADER); | ||
} | ||
|
||
/** | ||
* 获取启用的注入点为 FOOTER 的 HtmlInjection 对象. | ||
*/ | ||
public Flux<HtmlInjection> getFooterEnabledInjections() { | ||
return getAllEnabledInjections() | ||
.filter(htmlInjection -> | ||
htmlInjection.getSpec().getInjectionPoint() == HtmlInjection.InjectionPoint.FOOTER); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters