diff --git a/src/Components/Inlines/Email.php b/src/Components/Inlines/Email.php new file mode 100644 index 0000000..c012ab8 --- /dev/null +++ b/src/Components/Inlines/Email.php @@ -0,0 +1,67 @@ +text = $text; + $this->url = $url; + $this->width = $width; + } + + /** + * @param Excerpt $Excerpt + * @param State $State + * @return static|null + */ + public static function build(Excerpt $Excerpt, State $State) + { + $hostnameLabel = '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?'; + + $commonMarkEmail = '[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]++@' + . $hostnameLabel . '(?:\.' . $hostnameLabel . ')*'; + + if (\strpos($Excerpt->text(), '>') !== false + and \preg_match("/^<((mailto:)?$commonMarkEmail)>/i", $Excerpt->text(), $matches) + ) { + $url = $matches[1]; + + if (! isset($matches[2])) { + $url = "mailto:$url"; + } + + return new self($matches[1], $url, \strlen($matches[0])); + } + } + + /** + * @return Element + */ + public function stateRenderable(Parsedown $_) + { + return new Element('a', ['href' => $this->url], [new Text($this->text)]); + } +} diff --git a/src/Parsedown.php b/src/Parsedown.php index e4f57eb..c96c4d0 100644 --- a/src/Parsedown.php +++ b/src/Parsedown.php @@ -411,35 +411,6 @@ class Parsedown return $Elements; } - protected function inlineEmailTag($Excerpt) - { - $hostnameLabel = '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?'; - - $commonMarkEmail = '[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]++@' - . $hostnameLabel . '(?:\.' . $hostnameLabel . ')*'; - - if (\strpos($Excerpt['text'], '>') !== false - and \preg_match("/^<((mailto:)?$commonMarkEmail)>/i", $Excerpt['text'], $matches) - ) { - $url = $matches[1]; - - if (! isset($matches[2])) { - $url = "mailto:$url"; - } - - return [ - 'extent' => \strlen($matches[0]), - 'element' => [ - 'name' => 'a', - 'text' => $matches[1], - 'attributes' => [ - 'href' => $url, - ], - ], - ]; - } - } - protected function inlineEmphasis($Excerpt) { if (! isset($Excerpt['text'][1])) {