diff --git a/Extension.php b/Extension.php index f0a6a78..6ee6d7b 100644 --- a/Extension.php +++ b/Extension.php @@ -4,10 +4,10 @@ * * It creates a list of contents table from headings. * - * @version 20180122-1859 - * @author KEINOS (https://github.com/KEINOS/) - * @package Parsedown (https://github.com/erusev/parsedown) - * @see How to: https://github.com/KEINOS/parsedown-extension_table-of-contents/ + * @version 20180407-2203 + * @author KEINOS (https://github.com/KEINOS/) + * @package Parsedown 1.7.1 (https://github.com/erusev/parsedown) + * @see Howto: https://github.com/KEINOS/parsedown-extension_table-of-contents/ * @license https://github.com/KEINOS/parsedown-extension_table-of-contents/LICENSE */ class Extension extends Parsedown @@ -23,33 +23,44 @@ protected function createAnchorID($Text) } # - # Setters + # contents list # - function setContentsListAsArray(bool $outListAsArray) + function contentsList($Return_as = 'string') { - $this->contentsListAsArray = $outListAsArray; - - return $this; + if ('string' === strtolower($Return_as)) { + $result = ''; + if (! empty($this->contentsListString)) { + $result = $this->text($this->contentsListString); + } + return $result; + } elseif ('json' === strtolower($Return_as)) { + return json_encode($this->contentsListArray); + } else { + return $this->contentsListArray; + } } - protected $contentsListAsArray = false; - # - # contents list + # Setters # - function contentsList() + protected function setContentsList($Content) { - if(! empty($this->contentsListString)){ - return $this->text( $this->contentsListString ); - } + $this->setContentsListAsArray($Content); + $this->setContentsListAsString($Content); } - protected function setContentsList($Content) + protected function setContentsListAsArray($Content) + { + $this->contentsListArray[] = $Content; + } + protected $contentsListArray = array(); + + protected function setContentsListAsString($Content) { - $id = $Content['id']; - $text = $this->fetchText($Content['text']); - $link = "[${text}](#${id})"; - $level = (integer) trim($Content['level'],'h'); + $text = $this->fetchText($Content['text']); + $id = $Content['id']; + $level = (integer) trim($Content['level'], 'h'); + $link = "[${text}](#${id})"; if ($this->firstHeadLevel === 0) { $this->firstHeadLevel = $level; @@ -74,10 +85,9 @@ protected function setContentsList($Content) protected function blockHeader($Line) { if (isset($Line['text'][1])) { - $Block = Parsedown::blockHeader($Line); - $text = $Block['element']['text']; + $text = $Block['element']['handler']['argument']; $level = $Block['element']['name']; //h1,h2..h6 $id = $this->createAnchorID($text); @@ -96,5 +106,4 @@ protected function blockHeader($Line) return $Block; } } - }