Passed
Push — master ( af0510...079b22 )
by Stéphane
23:12
created

LinkRenderer::render()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 44
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 22
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 44
ccs 0
cts 24
cp 0
crap 42
rs 8.9457
1
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
2
3
use League\CommonMark\ElementRendererInterface;
4
use League\CommonMark\HtmlElement;
5
use League\CommonMark\Inline\Element\AbstractInline;
6
use League\CommonMark\Inline\Element\Link;
7
use Todaymade\Daux\DauxHelper;
8
9
class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
10
{
11
    /**
12
     * @param AbstractInline|Link $inline
13
     *
14
     * @return HtmlElement
15
     */
16
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
17
    {
18
        if (!($inline instanceof Link)) {
19
            throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
20
        }
21
22
        // Default handling
23
        $element = parent::render($inline, $htmlRenderer);
24
25
        $url = $inline->getUrl();
26
27
        // empty urls, anchors and absolute urls
28
        // should not go through the url resolver
29
        if (!DauxHelper::isValidUrl($url) || DauxHelper::isExternalUrl($url)) {
30
            return $element;
31
        }
32
33
        // if there's a hash component in the url, ensure we
34
        // don't put that part through the resolver.
35
        $urlAndHash = explode('#', $url);
36
        $url = $urlAndHash[0];
37
38
        //Internal links
39
        $file = DauxHelper::resolveInternalFile($this->daux, $url);
40
41
        $link_props = [];
42
        if (isset($urlAndHash[1])) {
43
            $link_props["ac:anchor"] = $urlAndHash[1];
44
        }
45
46
        $page_props = [
47
            'ri:content-title' => trim(trim($this->daux['confluence']['prefix']) . ' ' . $file->getTitle()),
48
            'ri:space-key' => $this->daux['confluence']['space_id'],
49
        ];
50
51
        $page = strval(new HtmlElement('ri:page', $page_props, '', true));
52
        $children = $htmlRenderer->renderInlines($inline->children());
53
        if (strpos($children, '<') !== false) {
54
            $children = '<ac:link-body>' . $children . '</ac:link-body>';
55
        } else {
56
            $children = '<ac:plain-text-link-body><![CDATA[' . $children . ']]></ac:plain-text-link-body>';
57
        }
58
59
        return new HtmlElement('ac:link', $link_props, $page . $children);
60
    }
61
}
62