@@ -67,11 +67,20 @@ public static function footer()
6767 {
6868
6969 echo '<script>
70- $(function(){
71- $("#toc-switch").click(function(){
72- $("#toc-ul").toggle("fast");
73- });
74- });
70+ window.content_index_showTocToggle = true;
71+ function content_index_toggleToc() {
72+ var tts = "显示";
73+ var tth = "隐藏";
74+ if (window.content_index_showTocToggle) {
75+ window.content_index_showTocToggle = false;
76+ document.getElementById("toc-content").style.display = "none";
77+ document.getElementById("content-index-togglelink").innerHTML = tts
78+ } else {
79+ window.content_index_showTocToggle = true;
80+ document.getElementById("toc-content").style.display = "block";
81+ document.getElementById("content-index-togglelink").innerHTML = tth
82+ }
83+ }
7584 </script> ' ;
7685 }
7786
@@ -87,12 +96,12 @@ public static function replace($content, $class, $string)
8796
8897 $ html_string = is_null ($ string ) ? $ content : $ string ;
8998
90- if ( $ class ->is ('index ' ) || $ class ->is ('search ' )){
99+ if ( $ class ->is ('index ' ) || $ class ->is ('search ' ) || $ class -> is ( ' date ' ) ){
91100 return $ html_string ;
92101 }
93102
94- $ toc = self ::create_toc ($ html_string );
95- return $ toc[ ' toc ' ] . $ toc [ ' content ' ] ;
103+ $ toc = self ::create_toc_with_dom ($ html_string );
104+ return $ toc ;
96105 }
97106
98107 /**
@@ -205,10 +214,63 @@ public static function create_toc($content)
205214 $ toc .= '</li> ' ."\n" ;
206215 $ toc .= '</ul></div> ' ."\n" ;
207216
208- return array (
209- 'toc ' => $ toc ,
210- 'content ' => $ content
211- );
217+ return $ toc . $ content ;
218+ }
219+
220+
221+ public static function create_toc_with_dom ($ content )
222+ {
223+ require_once 'simple_html_dom.php ' ;
224+
225+ $ html = str_get_html ($ content );
226+
227+ $ toc = '<div class="toc-index"><div class="toc-title">本文目录</div><span class="toc-toggle">[<a id="content-index-togglelink" href="javascript:content_index_toggleToc()">隐藏</a>]</span><div id="toc-content"> ' ;
228+ $ toc .= '' ;
229+ $ last_level = 0 ;
230+ $ count_h2 = 0 ;
231+ $ new = $ html ->find ('h2,h3 ' );
232+
233+ if (count ($ new ) < 3 ) return $ content ;
234+
235+ foreach ($ new as $ h ){
236+
237+
238+
239+ $ innerTEXT = trim ($ h ->innertext );
240+ $ id = str_replace (' ' ,'_ ' ,$ innerTEXT );
241+ $ level = intval ($ h ->tag [1 ]);
242+
243+ if ($ level == 2 ){
244+ $ count_h2 ++;
245+ $ innerTEXT = self ::dec2roman ($ count_h2 ) . ' ' . $ innerTEXT ;
246+ }
247+ $ h ->id = $ id ; // add id attribute so we can jump to this element
248+
249+ $ h ->innertext = $ innerTEXT ; // add id attribute so we can jump to this element
250+
251+
252+ if ($ level > $ last_level )
253+ // add class
254+ $ toc .= '<ol> ' ;
255+ else {
256+ $ toc .= str_repeat ('</li></ol> ' , $ last_level - $ level );
257+ $ toc .= '</li> ' ;
258+ }
259+ if ($ level >= $ last_level ){
260+ $ toc .= "<li class='toc-level $ level'><a href='# {$ id }'> {$ innerTEXT }</a> " ;
261+ }else {
262+ $ toc .= "<li><a href='# {$ id }'> {$ innerTEXT }</a> " ;
263+ }
264+
265+
266+ $ last_level = $ level ;
267+ }
268+
269+ $ toc .= str_repeat ('</li></ol> ' , $ last_level );
270+ $ toc .= '</div></div> ' ;
271+
272+ return $ toc . "<hr> " . $ html ->save ();
273+
212274 }
213275
214276
0 commit comments