44 *
55 * It creates a list of contents table from headings.
66 *
7- * @version 20180122-1859
8- * @author KEINOS (https://github.com/KEINOS/)
9- * @package Parsedown (https://github.com/erusev/parsedown)
10- * @see How to : https://github.com/KEINOS/parsedown-extension_table-of-contents/
7+ * @version 20180407-2203
8+ * @author KEINOS (https://github.com/KEINOS/)
9+ * @package Parsedown 1.7.1 (https://github.com/erusev/parsedown)
10+ * @see Howto : https://github.com/KEINOS/parsedown-extension_table-of-contents/
1111 * @license https://github.com/KEINOS/parsedown-extension_table-of-contents/LICENSE
1212 */
1313class Extension extends Parsedown
@@ -23,33 +23,44 @@ protected function createAnchorID($Text)
2323 }
2424
2525 #
26- # Setters
26+ # contents list
2727 #
28- function setContentsListAsArray ( bool $ outListAsArray )
28+ function contentsList ( $ Return_as = ' string ' )
2929 {
30- $ this ->contentsListAsArray = $ outListAsArray ;
31-
32- return $ this ;
30+ if ('string ' === strtolower ($ Return_as )) {
31+ $ result = '' ;
32+ if (! empty ($ this ->contentsListString )) {
33+ $ result = $ this ->text ($ this ->contentsListString );
34+ }
35+ return $ result ;
36+ } elseif ('json ' === strtolower ($ Return_as )) {
37+ return json_encode ($ this ->contentsListArray );
38+ } else {
39+ return $ this ->contentsListArray ;
40+ }
3341 }
3442
35- protected $ contentsListAsArray = false ;
36-
3743 #
38- # contents list
44+ # Setters
3945 #
40- function contentsList ( )
46+ protected function setContentsList ( $ Content )
4147 {
42- if (! empty ($ this ->contentsListString )){
43- return $ this ->text ( $ this ->contentsListString );
44- }
48+ $ this ->setContentsListAsArray ($ Content );
49+ $ this ->setContentsListAsString ($ Content );
4550 }
4651
47- protected function setContentsList ($ Content )
52+ protected function setContentsListAsArray ($ Content )
53+ {
54+ $ this ->contentsListArray [] = $ Content ;
55+ }
56+ protected $ contentsListArray = array ();
57+
58+ protected function setContentsListAsString ($ Content )
4859 {
49- $ id = $ Content ['id ' ] ;
50- $ text = $ this -> fetchText ( $ Content ['text ' ]) ;
51- $ link = " [ $ {text} ](# $ {id} ) " ;
52- $ level = ( integer ) trim ( $ Content [ ' level ' ], ' h ' ) ;
60+ $ text = $ this -> fetchText ( $ Content ['text ' ]) ;
61+ $ id = $ Content ['id ' ] ;
62+ $ level = ( integer ) trim ( $ Content [ ' level ' ], ' h ' ) ;
63+ $ link = " [ $ {text} ](# $ {id} ) " ;
5364
5465 if ($ this ->firstHeadLevel === 0 ) {
5566 $ this ->firstHeadLevel = $ level ;
@@ -74,10 +85,9 @@ protected function setContentsList($Content)
7485 protected function blockHeader ($ Line )
7586 {
7687 if (isset ($ Line ['text ' ][1 ])) {
77-
7888 $ Block = Parsedown::blockHeader ($ Line );
7989
80- $ text = $ Block ['element ' ]['text ' ];
90+ $ text = $ Block ['element ' ]['handler ' ][ ' argument ' ];
8191 $ level = $ Block ['element ' ]['name ' ]; //h1,h2..h6
8292 $ id = $ this ->createAnchorID ($ text );
8393
@@ -96,5 +106,4 @@ protected function blockHeader($Line)
96106 return $ Block ;
97107 }
98108 }
99-
100109}
0 commit comments