-
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.
Implement global HTML injection for head and footer (#9)
#### What type of PR is this? /kind feature #### What this PR does / why we need it: 此 PR 实现了代码全局的HEAD和FOOTER注入,暂时没有实现路径匹配的逻辑 #### Which issue(s) this PR fixes: Fixes #8 #### Does this PR introduce a user-facing change? ```release-note None ```
- Loading branch information
Showing
7 changed files
with
157 additions
and
3 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
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
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,36 @@ | ||
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.listEnabledInjectionsByPoint(HtmlInjection.InjectionPoint.FOOTER) | ||
.doOnNext(htmlInjection -> { | ||
if (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,35 @@ | ||
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.listEnabledInjectionsByPoint(HtmlInjection.InjectionPoint.HEADER) | ||
.doOnNext(htmlInjection -> { | ||
if (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,8 @@ | ||
package run.halo.injection; | ||
|
||
import reactor.core.publisher.Flux; | ||
|
||
public interface HtmlService { | ||
|
||
Flux<HtmlInjection> listEnabledInjectionsByPoint(HtmlInjection.InjectionPoint injectionPoint); | ||
} |
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 static run.halo.app.extension.index.query.QueryFactory.and; | ||
import static run.halo.app.extension.index.query.QueryFactory.equal; | ||
|
||
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.Query; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class HtmlServiceImpl implements HtmlService { | ||
private final ReactiveExtensionClient client; | ||
|
||
/** | ||
* 根据注入点获取正在启用的HtmlInjection 对象. | ||
*/ | ||
@Override | ||
public Flux<HtmlInjection> listEnabledInjectionsByPoint( | ||
HtmlInjection.InjectionPoint injectionPoint) { | ||
Query query = and( | ||
equal("spec.enabled", "true"), | ||
equal("spec.injectionPoint", injectionPoint.name()) | ||
); | ||
|
||
ListOptions options = ListOptions.builder() | ||
.fieldQuery(query) | ||
.build(); | ||
|
||
return client.listAll(HtmlInjection.class, options, | ||
Sort.by(Sort.Order.asc("metadata.name"))); | ||
} | ||
} | ||
|
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