diff --git a/src/main/java/xin/wenjing/blogHao/entity/Settings.java b/src/main/java/xin/wenjing/blogHao/entity/Settings.java index 6a4c2b8..64a9ddd 100644 --- a/src/main/java/xin/wenjing/blogHao/entity/Settings.java +++ b/src/main/java/xin/wenjing/blogHao/entity/Settings.java @@ -65,7 +65,9 @@ public static class ContentSpace{ @Data public static class ContentIndent{ private boolean enableContentIndent; - private String indentNodeName; + private String postIndentNodeName; + private String pageIndentNodeName; + private String isOnlyPostIndent; } /** diff --git a/src/main/java/xin/wenjing/blogHao/processor/MiniToolsProcessor.java b/src/main/java/xin/wenjing/blogHao/processor/MiniToolsProcessor.java index 3d705b4..c55331c 100644 --- a/src/main/java/xin/wenjing/blogHao/processor/MiniToolsProcessor.java +++ b/src/main/java/xin/wenjing/blogHao/processor/MiniToolsProcessor.java @@ -2,6 +2,7 @@ import lombok.AllArgsConstructor; 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; @@ -11,6 +12,7 @@ import run.halo.app.theme.dialect.TemplateHeadProcessor; import xin.wenjing.blogHao.entity.Settings; import xin.wenjing.blogHao.util.ScriptContentUtils; +import java.util.Properties; /** * 小工具脚本注入 @@ -23,29 +25,57 @@ public class MiniToolsProcessor implements TemplateHeadProcessor { private final ReactiveSettingFetcher settingFetcher; + static final PropertyPlaceholderHelper PROPERTY_PLACEHOLDER_HELPER = new PropertyPlaceholderHelper("${", "}"); + + @Override public Mono process(ITemplateContext context, IModel model, IElementModelStructureHandler structureHandler) { return settingFetcher.fetch(Settings.MiniTool.GROUP_NAME, Settings.MiniTool.class) .doOnNext( miniTool -> { - String scriptRes = miniToolsScript( miniTool); + String scriptRes = miniToolsScript(miniTool, context); final IModelFactory modelFactory = context.getModelFactory(); model.add(modelFactory.createText(scriptRes)); }).then(); } - private String miniToolsScript(Settings.MiniTool miniTool) { + private String miniToolsScript(Settings.MiniTool miniTool, ITemplateContext context) { + + final Properties contentIndentProperties = new Properties(); + contentIndentProperties.setProperty("postNodeName", miniTool.getContentIndent().getPostIndentNodeName()); + + StringBuilder injectCode = new StringBuilder(); - StringBuilder injectCode = new StringBuilder(""); // 中英文空格脚本 if(miniTool.getContentSpace().isEnableContentSpace()){ injectCode.append(ScriptContentUtils.panguScript(miniTool)); } + // 段落内容首行缩进 if(miniTool.getContentIndent().isEnableContentIndent()){ - injectCode.append(""" - - """.formatted(miniTool.getContentIndent().getIndentNodeName())); + String templateId = ScriptContentUtils.getTemplateId(context); + final String postIndentStyle = """ + + """; + final String allIndentStyle = """ + + """; + if(miniTool.getContentIndent().getIsOnlyPostIndent().equals("onlyPost") && templateId.equals("post")){ + injectCode.append(PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders(postIndentStyle, contentIndentProperties)); + }else if(miniTool.getContentIndent().getIsOnlyPostIndent().equals("globalPage")){ + contentIndentProperties.setProperty("pageNodeName", miniTool.getContentIndent().getPageIndentNodeName()); + injectCode.append(PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders(allIndentStyle, contentIndentProperties)); + } } + return injectCode.toString(); } diff --git a/src/main/java/xin/wenjing/blogHao/util/ScriptContentUtils.java b/src/main/java/xin/wenjing/blogHao/util/ScriptContentUtils.java index c8a8814..0c0b97e 100644 --- a/src/main/java/xin/wenjing/blogHao/util/ScriptContentUtils.java +++ b/src/main/java/xin/wenjing/blogHao/util/ScriptContentUtils.java @@ -109,7 +109,7 @@ public static String panguScript(Settings.MiniTool config) { document.addEventListener("DOMContentLoaded", function() { pangu.autoSpacingPage(); }) - + """.formatted(config.getContentSpace().getScanContent()); } diff --git a/src/main/resources/extensions/settings.yaml b/src/main/resources/extensions/settings.yaml index 3fed32f..8ced7a9 100644 --- a/src/main/resources/extensions/settings.yaml +++ b/src/main/resources/extensions/settings.yaml @@ -236,10 +236,28 @@ spec: value: false - $formkit: text if: $get(enableContentIndent).value - label: 内容缩进的节点类名或者ID名 - name: indentNodeName + label: 文章内容缩进的节点类名或者ID名 + name: postIndentNodeName value: "#post" - help: 根据自己使用的主题选择要适配的内容节点 + help: 根据自己使用的主题文章节点选择要适配的内容节点 + - $formkit: text + if: $get(enableContentIndent).value + label: 单页面内容缩进的节点类名或者ID名 + name: pageIndentNodeName + value: "#page" + help: 根据自己使用的主题单页面节点选择要适配的内容节点 + - $formkit: select + if: $get(enableContentIndent).value + name: isOnlyPostIndent + id: isOnlyPostIndent + key: isOnlyPostIndent + label: 是否仅针对文章页面生效 + value: globalPage + options: + - value: onlyPost + label: 仅针对文章页面 + - value: globalPage + label: 所有页面 - $formkit: group name: colorless label: 站点失色