aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/league/html-to-markdown/src/Converter/TextConverter.php
blob: d6d91e16fa8379f3c762f1208b5012d4125714da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

namespace League\HTMLToMarkdown\Converter;

use League\HTMLToMarkdown\ElementInterface;

class TextConverter implements ConverterInterface
{
    /**
     * @param ElementInterface $element
     *
     * @return string
     */
    public function convert(ElementInterface $element)
    {
        $markdown = $element->getValue();

        // Remove leftover \n at the beginning of the line
        $markdown = ltrim($markdown, "\n");

        // Replace sequences of invisible characters with spaces
        $markdown = preg_replace('~\s+~u', ' ', $markdown);

        // Escape the following characters: '*', '_', '[', ']' and '\'
        $markdown = preg_replace('~([*_\\[\\]\\\\])~u', '\\\\$1', $markdown);

        $markdown = preg_replace('~^#~u', '\\\\#', $markdown);

        if ($markdown === ' ') {
            $next = $element->getNext();
            if (!$next || $next->isBlock()) {
                $markdown = '';
            }
        }

        return $markdown;
    }

    /**
     * @return string[]
     */
    public function getSupportedTags()
    {
        return array('#text');
    }
}