From 07c2566042b574a7ce3ba104a6b57fa0a77b1a8a Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 20 Jan 2019 02:27:10 +0000 Subject: [PATCH] Implement Header --- src/Components/Blocks/Header.php | 84 ++++++++++++++++++++++++++++++++ src/Parsedown.php | 33 ------------- 2 files changed, 84 insertions(+), 33 deletions(-) create mode 100644 src/Components/Blocks/Header.php diff --git a/src/Components/Blocks/Header.php b/src/Components/Blocks/Header.php new file mode 100644 index 0000000..bf93ddb --- /dev/null +++ b/src/Components/Blocks/Header.php @@ -0,0 +1,84 @@ +text = $text; + $this->level = $level; + } + + /** + * @param Context $Context + * @param Block|null $Block + * @param State|null $State + * @return static|null + */ + public static function build( + Context $Context, + Block $Block = null, + State $State = null + ) { + $State = $State ?: new State; + + $level = \strspn($Context->line()->text(), '#'); + + if ($level > 6 || $level < 1) { + return null; + } + + /** @var 1|2|3|4|5|6 $level */ + + $text = \trim($Context->line()->text(), '#'); + + $StrictMode = $State->getOrDefault(StrictMode::class); + + if ($StrictMode->enabled() && isset($text[0]) and $text[0] !== ' ') { + return null; + } + + $text = \trim($text, ' '); + + return new self($text, $level); + } + + /** + * @return Handler + */ + public function stateRenderable(Parsedown $Parsedown) + { + return new Handler( + /** @return Element */ + function (State $State) use ($Parsedown) { + return new Element( + 'h' . \strval($this->level), + [], + $State->applyTo($Parsedown->lineElements($this->text)) + ); + } + ); + } +} diff --git a/src/Parsedown.php b/src/Parsedown.php index b7787ac..1f3dc4f 100644 --- a/src/Parsedown.php +++ b/src/Parsedown.php @@ -334,39 +334,6 @@ class Parsedown return $Block; } - # - # Header - - protected function blockHeader(Context $Context) - { - $level = \strspn($Context->line()->text(), '#'); - - if ($level > 6) { - return; - } - - $text = \trim($Context->line()->text(), '#'); - - if ($this->strictMode and isset($text[0]) and $text[0] !== ' ') { - return; - } - - $text = \trim($text, ' '); - - $Block = [ - 'element' => [ - 'name' => 'h' . $level, - 'handler' => [ - 'function' => 'lineElements', - 'argument' => $text, - 'destination' => 'elements', - ] - ], - ]; - - return $Block; - } - # # List