From bf0424323397fae9126b7382be21e02a31b2f58f Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Tue, 5 Mar 2024 18:34:08 +0800 Subject: [PATCH] refactor: move comment lib script location to head tag (#87) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 script 移动至 head 标签,以解决主题使用 pjax 或者同页面多评论组件(瞬间)的问题。 Fixes #86 ```release-note 全局加载评论组件,解决部分主题使用 pjax 库之后的评论组件加载问题。 ``` --- .../widget/CommentWidgetHeadProcessor.java | 44 +++++++++++++++++++ .../comment/widget/DefaultCommentWidget.java | 2 - 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/main/java/run/halo/comment/widget/CommentWidgetHeadProcessor.java diff --git a/src/main/java/run/halo/comment/widget/CommentWidgetHeadProcessor.java b/src/main/java/run/halo/comment/widget/CommentWidgetHeadProcessor.java new file mode 100644 index 0000000..4407ea9 --- /dev/null +++ b/src/main/java/run/halo/comment/widget/CommentWidgetHeadProcessor.java @@ -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 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(""" + + + + + """, properties); + } +} diff --git a/src/main/java/run/halo/comment/widget/DefaultCommentWidget.java b/src/main/java/run/halo/comment/widget/DefaultCommentWidget.java index 133736b..5eb0f5e 100644 --- a/src/main/java/run/halo/comment/widget/DefaultCommentWidget.java +++ b/src/main/java/run/halo/comment/widget/DefaultCommentWidget.java @@ -65,8 +65,6 @@ private String commentHtml(IAttribute groupAttribute, IAttribute kindAttribute, return PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders("""
- -