generated from halo-dev/plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move comment lib script location to head tag
Signed-off-by: Ryan Wang <[email protected]>
- Loading branch information
Showing
2 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/run/halo/comment/widget/CommentWidgetHeadProcessor.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,44 @@ | ||
package run.halo.comment.widget; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.pf4j.PluginWrapper; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.PropertyPlaceholderHelper; | ||
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.theme.dialect.TemplateHeadProcessor; | ||
|
||
import java.util.Properties; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CommentWidgetHeadProcessor implements TemplateHeadProcessor { | ||
|
||
static final PropertyPlaceholderHelper PROPERTY_PLACEHOLDER_HELPER = new PropertyPlaceholderHelper("${", "}"); | ||
|
||
private final PluginWrapper pluginWrapper; | ||
|
||
@Override | ||
public Mono<Void> process(ITemplateContext context, IModel model, | ||
IElementModelStructureHandler structureHandler) { | ||
final IModelFactory modelFactory = context.getModelFactory(); | ||
model.add(modelFactory.createText(commentWidgetScript())); | ||
return Mono.empty(); | ||
} | ||
|
||
private String commentWidgetScript() { | ||
|
||
final Properties properties = new Properties(); | ||
properties.setProperty("version", pluginWrapper.getDescriptor().getVersion()); | ||
|
||
return PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders(""" | ||
<!-- plugin-comment-widget start --> | ||
<script src="/plugins/PluginCommentWidget/assets/static/comment-widget.iife.js?version=${version}"></script> | ||
<link rel="stylesheet" href="/plugins/PluginCommentWidget/assets/static/style.css?version=${version}" /> | ||
<!-- plugin-comment-widget end --> | ||
""", properties); | ||
} | ||
} |
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