]> git.mxchange.org Git - friendica.git/blob - vendor/league/html-to-markdown/src/Converter/TextConverter.php
Move HTML to Markdown library to Composer
[friendica.git] / vendor / league / html-to-markdown / src / Converter / TextConverter.php
1 <?php
2
3 namespace League\HTMLToMarkdown\Converter;
4
5 use League\HTMLToMarkdown\ElementInterface;
6
7 class TextConverter implements ConverterInterface
8 {
9     /**
10      * @param ElementInterface $element
11      *
12      * @return string
13      */
14     public function convert(ElementInterface $element)
15     {
16         $markdown = $element->getValue();
17
18         // Remove leftover \n at the beginning of the line
19         $markdown = ltrim($markdown, "\n");
20
21         // Replace sequences of invisible characters with spaces
22         $markdown = preg_replace('~\s+~u', ' ', $markdown);
23
24         // Escape the following characters: '*', '_', '[', ']' and '\'
25         $markdown = preg_replace('~([*_\\[\\]\\\\])~u', '\\\\$1', $markdown);
26
27         $markdown = preg_replace('~^#~u', '\\\\#', $markdown);
28
29         if ($markdown === ' ') {
30             $next = $element->getNext();
31             if (!$next || $next->isBlock()) {
32                 $markdown = '';
33             }
34         }
35
36         return $markdown;
37     }
38
39     /**
40      * @return string[]
41      */
42     public function getSupportedTags()
43     {
44         return array('#text');
45     }
46 }