From a485b3f5282331f0722c9f2d4a4eda00ca5339f7 Mon Sep 17 00:00:00 2001 From: wenjing-xin Date: Sun, 28 Jul 2024 10:10:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=86=85=E5=AE=B9=E7=BC=A9?= =?UTF-8?q?=E8=BF=9B=E5=8A=9F=E8=83=BD=E4=B8=BAjs=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xin/wenjing/blogHao/entity/Settings.java | 6 ++ .../blogHao/processor/MiniToolsProcessor.java | 55 +++++++++++++------ src/main/resources/extensions/settings.yaml | 15 +++++ .../resources/static/custom/textIndent.js | 35 ++++++++++++ 4 files changed, 93 insertions(+), 18 deletions(-) create mode 100644 src/main/resources/static/custom/textIndent.js diff --git a/src/main/java/xin/wenjing/blogHao/entity/Settings.java b/src/main/java/xin/wenjing/blogHao/entity/Settings.java index 64a9ddd..0f9c259 100644 --- a/src/main/java/xin/wenjing/blogHao/entity/Settings.java +++ b/src/main/java/xin/wenjing/blogHao/entity/Settings.java @@ -68,6 +68,12 @@ public static class ContentIndent{ private String postIndentNodeName; private String pageIndentNodeName; private String isOnlyPostIndent; + private List excludeNodeList; + } + + @Data + public static class ExcludeNodeList{ + private String nodeName; } /** diff --git a/src/main/java/xin/wenjing/blogHao/processor/MiniToolsProcessor.java b/src/main/java/xin/wenjing/blogHao/processor/MiniToolsProcessor.java index c55331c..387bf3e 100644 --- a/src/main/java/xin/wenjing/blogHao/processor/MiniToolsProcessor.java +++ b/src/main/java/xin/wenjing/blogHao/processor/MiniToolsProcessor.java @@ -8,10 +8,14 @@ import org.thymeleaf.model.IModelFactory; import org.thymeleaf.processor.element.IElementModelStructureHandler; import reactor.core.publisher.Mono; +import run.halo.app.plugin.PluginContext; import run.halo.app.plugin.ReactiveSettingFetcher; import run.halo.app.theme.dialect.TemplateHeadProcessor; import xin.wenjing.blogHao.entity.Settings; import xin.wenjing.blogHao.util.ScriptContentUtils; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Properties; /** @@ -27,6 +31,7 @@ public class MiniToolsProcessor implements TemplateHeadProcessor { static final PropertyPlaceholderHelper PROPERTY_PLACEHOLDER_HELPER = new PropertyPlaceholderHelper("${", "}"); + private final PluginContext pluginContext; @Override public Mono process(ITemplateContext context, IModel model, IElementModelStructureHandler structureHandler) { @@ -42,7 +47,14 @@ private String miniToolsScript(Settings.MiniTool miniTool, ITemplateContext cont final Properties contentIndentProperties = new Properties(); contentIndentProperties.setProperty("postNodeName", miniTool.getContentIndent().getPostIndentNodeName()); + contentIndentProperties.setProperty("pageNodeName", miniTool.getContentIndent().getPageIndentNodeName()); + contentIndentProperties.setProperty("version", pluginContext.getVersion()); + List excludeNodeName = new ArrayList<>(); + miniTool.getContentIndent().getExcludeNodeList().stream().forEach(item -> { + excludeNodeName.add(item.getNodeName()); + }); + contentIndentProperties.setProperty("excludeEle", excludeNodeName.toString()); StringBuilder injectCode = new StringBuilder(); // 中英文空格脚本 @@ -52,28 +64,35 @@ private String miniToolsScript(Settings.MiniTool miniTool, ITemplateContext cont // 段落内容首行缩进 if(miniTool.getContentIndent().isEnableContentIndent()){ + String templateId = ScriptContentUtils.getTemplateId(context); - final String postIndentStyle = """ - - """; - final String allIndentStyle = """ - - """; + + final String jsScript = """ + + + """; if(miniTool.getContentIndent().getIsOnlyPostIndent().equals("onlyPost") && templateId.equals("post")){ - injectCode.append(PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders(postIndentStyle, contentIndentProperties)); + contentIndentProperties.setProperty("isOnlyPost", "true"); }else if(miniTool.getContentIndent().getIsOnlyPostIndent().equals("globalPage")){ - contentIndentProperties.setProperty("pageNodeName", miniTool.getContentIndent().getPageIndentNodeName()); - injectCode.append(PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders(allIndentStyle, contentIndentProperties)); + contentIndentProperties.setProperty("isOnlyPost", "false"); } + String executeJs = PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders(jsScript, contentIndentProperties); + injectCode.append(executeJs); } return injectCode.toString(); diff --git a/src/main/resources/extensions/settings.yaml b/src/main/resources/extensions/settings.yaml index 8ced7a9..a42edf0 100644 --- a/src/main/resources/extensions/settings.yaml +++ b/src/main/resources/extensions/settings.yaml @@ -258,6 +258,21 @@ spec: label: 仅针对文章页面 - value: globalPage label: 所有页面 + - $formkit: repeater + if: $get(enableContentIndent).value + id: excludeNodeList + key: excludeNodeList + name: excludeNodeList + label: 排除缩进的一些元素 + help: 比如一些图片元素被包裹在p标签里,那么就填写img,对其进行排除或者排除p标签内含有某个类名、id名的属性 + value: [ ] + max: 12 + min: 0 + children: + - $formkit: text + name: nodeName + value: + label: 排除的节点名称 - $formkit: group name: colorless label: 站点失色 diff --git a/src/main/resources/static/custom/textIndent.js b/src/main/resources/static/custom/textIndent.js new file mode 100644 index 0000000..19b31fb --- /dev/null +++ b/src/main/resources/static/custom/textIndent.js @@ -0,0 +1,35 @@ +; +(function (){ + const executeTextIndent = function (node, excludeEleObj, isOnlyPost){ + + const allPostEle = document.querySelectorAll(node.postNodeName + " p:not(li>p):not(blockquote>p"); + let allPageEle = [] + if(!Boolean(isOnlyPost)){ + allPageEle = document.querySelectorAll(node.pageNodeName + " p:not(li>p):not(blockquote>p"); + } + let excludeEleList = []; + if(excludeEleObj && excludeEleObj.length){ + let resolveExcludeNodeList = excludeEleObj.substring(1, excludeEleObj.length -1).split(",").map(item=> item.toString().trim()); + excludeEleList.push(...resolveExcludeNodeList); + } + allPostEle.forEach(item => { + excludeEleList.forEach(excludeItem => { + if (item.innerHTML.toString().indexOf(excludeItem) == -1) { + item.style.textIndent = '2em'; + }else{ + item.style.textIndent = '0em'; + } + }); + }); + allPageEle.forEach(item => { + excludeEleList.forEach(excludeItem => { + if (item.innerHTML.toString().indexOf(excludeItem) == -1) { + item.style.textIndent = '2em'; + }else{ + item.style.textIndent = '0em'; + } + }); + }); + } + window.executeTextIndent = executeTextIndent; +})(); \ No newline at end of file