Skip to content

Commit ac26602

Browse files
committed
排除没有文章内容的页面不生成目录
1 parent 7dfbddf commit ac26602

3 files changed

Lines changed: 10 additions & 14 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=1.0.0-SNAPSHOT
1+
version=1.0.0-alpha.2

src/main/java/run/halo/tocbot/TocbotHeadProcessor.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public Mono<Void> process(ITemplateContext context, IModel model, IElementModelS
3939

4040
private String tocbotScript(BasicConfig config) {
4141

42-
String headerElVar = buildHeadersVar(config.getPostContent(), config.getHeaderEl());
4342
String tocbotInit = buildTocInit(config);
4443
// language=html
4544

@@ -48,7 +47,12 @@ private String tocbotScript(BasicConfig config) {
4847
<link href="/plugins/PluginTocbot/assets/static/tocbot/4.18.2/tocbot.css" rel="stylesheet">
4948
<script>
5049
document.addEventListener("DOMContentLoaded", function() {
51-
%s
50+
51+
const postContent = document.querySelector('%s');
52+
53+
if (postContent == null) return;
54+
55+
const headers = postContent.querySelectorAll('%s');
5256
// 没有 toc 目录,则直接移除
5357
if (headers.length === 0) {
5458
document.getElementById("%s").remove();
@@ -58,7 +62,7 @@ private String tocbotScript(BasicConfig config) {
5862
})
5963
</script>
6064
61-
""".formatted(headerElVar, config.getRemoveContentId(), tocbotInit);
65+
""".formatted(config.getPostContent(), config.getHeaderEl(), config.getRemoveContentId(), tocbotInit);
6266
}
6367

6468

@@ -85,14 +89,6 @@ private String buildTocInit(BasicConfig config) {
8589

8690
}
8791

88-
/**
89-
* 获取 headers
90-
*/
91-
private String buildHeadersVar(String postContent, String headerEl) {
92-
// language=javascript
93-
return "const headers = document.querySelector('%s').querySelectorAll('%s');".formatted(postContent, headerEl);
94-
}
95-
9692
/**
9793
* 基础配置
9894
*/

src/main/resources/plugin.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ metadata:
44
name: PluginTocbot
55
spec:
66
enabled: true
7-
version: 1.0.0-SNAPSHOT
7+
version: 1.0.0-alpha.2
88
requires: "*"
99
author: halo-dev
1010
logo: https://halo.run/logo
1111
settingName: plugin-tocbot-settings
1212
configMapName: plugin-tocbot-configMap
1313
homepage: https://github.com/liuzhihang/plugin-tocbot
14-
displayName: "Tocbot 目录生成插件"
14+
displayName: "Tocbot 目录生成"
1515
description: "对 Tocbot 的集成,支持生成文章目录"
1616
license:
1717
- name: "MIT"

0 commit comments

Comments
 (0)