@@ -294,34 +294,46 @@ public function body($text, $omit_toc_tag = false)
294294 }
295295
296296 /**
297- * Returns the parsed ToC.
298- * If the arg is "string" then it returns the ToC in HTML string.
297+ * Returns the parsed ToC in various formats.
299298 *
300- * @param string $type_return Type of the return format. "string", "json", "flatarray" and "nestedarray" .
299+ * If the arg is empty or "html" then it returns the ToC in HTML string .
301300 *
302- * @return false|string|array ToC in HTML/JSON format string or array.
301+ * @param string $type_return Type of the return format. Available types: "markdown", "html", "json", "flatarray" and "nestedarray". Default: "html". Aliases: "string" = "html", "md" = "markdown".
302+ *
303+ * @return false|string|array ToC in HTML/JSON format string or PHP array.
303304 */
304- public function contentsList ($ type_return = 'string ' )
305+ public function contentsList ($ type_return = 'html ' )
305306 {
306- if ('string ' === strtolower ($ type_return )) {
307+ $ type_return = strtolower ($ type_return );
308+
309+ if ('markdown ' === $ type_return || 'md ' === $ type_return ) {
307310 $ result = '' ;
308311 if (! empty ($ this ->contentsListString )) {
309- // Parses the ToC list in markdown to HTML
312+ $ result = $ this ->contentsListString ;
313+ }
314+
315+ return rtrim ($ result );
316+ }
317+
318+ if ('html ' === $ type_return || 'string ' === $ type_return ) {
319+ $ result = '' ;
320+ if (! empty ($ this ->contentsListString )) {
321+ // Parses the markdown ToC list to HTML
310322 $ result = $ this ->body ($ this ->contentsListString );
311323 }
312324
313325 return $ result ;
314326 }
315327
316- if ('json ' === strtolower ( $ type_return) ) {
328+ if ('json ' === $ type_return ) {
317329 return json_encode ($ this ->contentsListArray );
318330 }
319331
320- if ('flatarray ' === strtolower ( $ type_return) ) {
332+ if ('flatarray ' === $ type_return ) {
321333 return $ this ->contentsListArray ;
322334 }
323335
324- if ('nestedarray ' === strtolower ( $ type_return) ) {
336+ if ('nestedarray ' === $ type_return ) {
325337 return $ this ->buildNestedToc ($ this ->contentsListArray );
326338 }
327339
@@ -332,7 +344,7 @@ public function contentsList($type_return = 'string')
332344 . ' in Line: ' . __LINE__ . ' (Using default type) '
333345 );
334346
335- return $ this ->contentsList ('string ' );
347+ return $ this ->contentsList ('html ' );
336348 }
337349
338350 /**
@@ -451,6 +463,8 @@ protected function getSalt()
451463 /**
452464 * Gets the markdown tag for ToC.
453465 *
466+ * It returns the current tag to search for the ToC tag, '[toc]' for example, in the markdown text.
467+ *
454468 * @return string
455469 */
456470 protected function getTagToC ()
@@ -530,7 +544,7 @@ protected function setContentsListAsString(array $Content)
530544 $ level = $ level - $ cutIndent ;
531545 }
532546
533- $ indent = str_repeat (' ' , $ level );
547+ $ indent = str_repeat (' ' , $ level- 1 );
534548
535549 // Stores in markdown list format as below:
536550 // - [Header1](#Header1)
0 commit comments