diff --git a/src/Components/Inlines/Image.php b/src/Components/Inlines/Image.php new file mode 100644 index 0000000..a5178d5 --- /dev/null +++ b/src/Components/Inlines/Image.php @@ -0,0 +1,81 @@ +Link = $Link; + $this->width = $Link->width() + 1; + } + + /** + * @param Excerpt $Excerpt + * @param State $State + * @return static|null + */ + public static function build(Excerpt $Excerpt, State $State) + { + if (\substr($Excerpt->text(), 0, 1) !== '!') { + return null; + } + + $Excerpt = $Excerpt->addingToOffset(1); + + $Link = Link::build($Excerpt, $State); + + if (! isset($Link)) { + return null; + } + + return new self($Link); + } + + /** + * @return Handler + */ + public function stateRenderable(Parsedown $Parsedown) + { + return new Handler( + /** @return Element|Text */ + function (State $State) use ($Parsedown) { + $linkAttributes = $this->Link->attributes(); + + $attributes = [ + 'src' => $linkAttributes['href'], + 'alt' => $this->Link->label(), + ]; + + if (isset($linkAttributes['title'])) { + $attributes['title'] = $linkAttributes['title']; + } + + if ($State->getOrDefault(SafeMode::class)->enabled()) { + $attributes['src'] = Element::filterUnsafeUrl($attributes['src']); + } + + return Element::selfClosing('img', $attributes); + } + ); + } +} diff --git a/src/Parsedown.php b/src/Parsedown.php index fc69bad..656a677 100644 --- a/src/Parsedown.php +++ b/src/Parsedown.php @@ -411,39 +411,6 @@ class Parsedown return $Elements; } - protected function inlineImage($Excerpt) - { - if (! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[') { - return; - } - - $Excerpt['text']= \substr($Excerpt['text'], 1); - - $Link = $this->inlineLink($Excerpt); - - if ($Link === null) { - return; - } - - $Inline = [ - 'extent' => $Link['extent'] + 1, - 'element' => [ - 'name' => 'img', - 'attributes' => [ - 'src' => $Link['element']['attributes']['href'], - 'alt' => $Link['element']['handler']['argument'], - ], - 'autobreak' => true, - ], - ]; - - $Inline['element']['attributes'] += $Link['element']['attributes']; - - unset($Inline['element']['attributes']['href']); - - return $Inline; - } - protected function inlineMarkup($Excerpt) { if ($this->markupEscaped or $this->safeMode or \strpos($Excerpt['text'], '>') === false) {