1 | <?php |
||
7 | class LinkConverter implements ConverterInterface |
||
8 | { |
||
9 | /** |
||
10 | * @param ElementInterface $element |
||
11 | * |
||
12 | * @return string |
||
13 | */ |
||
14 | 3 | public function convert(ElementInterface $element) |
|
15 | { |
||
16 | 3 | $href = $element->getAttribute('href'); |
|
17 | 3 | $title = $element->getAttribute('title'); |
|
18 | 3 | $text = trim($element->getValue()); |
|
19 | |||
20 | 3 | if ($title !== '') { |
|
21 | 3 | $markdown = '[' . $text . '](' . $href . ' "' . $title . '")'; |
|
22 | 3 | } elseif ($href === $text && $this->isValidAutolink($href)) { |
|
23 | 3 | $markdown = '<' . $href . '>'; |
|
24 | 3 | } else { |
|
25 | 3 | $markdown = '[' . $text . '](' . $href . ')'; |
|
26 | } |
||
27 | |||
28 | 3 | if (!$href) { |
|
29 | 3 | $markdown = html_entity_decode($element->getChildrenAsString()); |
|
30 | 3 | } |
|
31 | |||
32 | 3 | return $markdown; |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * @return string[] |
||
37 | */ |
||
38 | 81 | public function getSupportedTags() |
|
42 | |||
43 | /** |
||
44 | * @param string $href |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | 3 | private function isValidAutolink($href) |
|
52 | } |
||
53 |