@@ -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 */
0 commit comments