Skip to content

Commit

Permalink
新增内容首行缩进作用范围
Browse files Browse the repository at this point in the history
  • Loading branch information
wenjing-xin committed Jul 27, 2024
1 parent ce00c9d commit 384adf8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/main/java/xin/wenjing/blogHao/entity/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
* 小工具脚本注入
Expand All @@ -23,29 +25,57 @@ public class MiniToolsProcessor implements TemplateHeadProcessor {

private final ReactiveSettingFetcher settingFetcher;

static final PropertyPlaceholderHelper PROPERTY_PLACEHOLDER_HELPER = new PropertyPlaceholderHelper("${", "}");


@Override
public Mono<Void> 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("""
<style type="text/css"> %s p:not(li>p):not(blockquote>p){text-indent: 2em;} </style>
""".formatted(miniTool.getContentIndent().getIndentNodeName()));
String templateId = ScriptContentUtils.getTemplateId(context);
final String postIndentStyle = """
<style type="text/css">
${postNodeName} p:not(li>p):not(blockquote>p){text-indent: 2em;}
</style>
""";
final String allIndentStyle = """
<style type="text/css">
${postNodeName} p:not(li>p):not(blockquote>p){
text-indent: 2em;
}
${pageNodeName} p:not(li>p):not(blockquote>p){
text-indent: 2em;
}
</style>
""";
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static String panguScript(Settings.MiniTool config) {
document.addEventListener("DOMContentLoaded", function() {
pangu.autoSpacingPage();
})
</script>
</script>
""".formatted(config.getContentSpace().getScanContent());
}

Expand Down
24 changes: 21 additions & 3 deletions src/main/resources/extensions/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: 站点失色
Expand Down

0 comments on commit 384adf8

Please sign in to comment.