2015-07-28 17:25:03 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
|
2015-06-29 16:16:39 +02:00
|
|
|
|
2015-10-21 23:26:42 +02:00
|
|
|
use League\CommonMark\ElementRendererInterface;
|
2017-06-07 23:40:12 +02:00
|
|
|
use League\CommonMark\HtmlElement;
|
2015-06-29 16:16:39 +02:00
|
|
|
use League\CommonMark\Inline\Element\AbstractInline;
|
|
|
|
use League\CommonMark\Inline\Element\Link;
|
2019-11-30 20:40:42 +01:00
|
|
|
use Todaymade\Daux\DauxHelper;
|
2015-06-29 16:16:39 +02:00
|
|
|
|
2015-07-29 22:31:41 +02:00
|
|
|
class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
|
2015-06-29 16:16:39 +02:00
|
|
|
{
|
|
|
|
/**
|
2016-09-15 00:49:48 +02:00
|
|
|
* @param AbstractInline|Link $inline
|
2015-10-21 23:26:42 +02:00
|
|
|
* @param ElementRendererInterface $htmlRenderer
|
2015-06-29 16:16:39 +02:00
|
|
|
*
|
|
|
|
* @return HtmlElement
|
|
|
|
*/
|
2015-10-21 23:26:42 +02:00
|
|
|
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
|
2015-06-29 16:16:39 +02:00
|
|
|
{
|
2019-11-30 22:24:10 +01:00
|
|
|
if (!($inline instanceof Link)) {
|
|
|
|
throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
|
2015-07-20 15:59:52 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 16:16:39 +02:00
|
|
|
// Default handling
|
|
|
|
$element = parent::render($inline, $htmlRenderer);
|
2016-09-15 00:49:48 +02:00
|
|
|
|
2015-06-29 16:16:39 +02:00
|
|
|
$url = $inline->getUrl();
|
2016-09-15 00:49:48 +02:00
|
|
|
|
|
|
|
// empty urls, anchors and absolute urls
|
|
|
|
// should not go through the url resolver
|
2019-11-30 22:24:10 +01:00
|
|
|
if (!DauxHelper::isValidUrl($url) || DauxHelper::isExternalUrl($url)) {
|
2015-06-29 16:16:39 +02:00
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Internal links
|
2019-11-30 20:40:42 +01:00
|
|
|
$file = DauxHelper::resolveInternalFile($this->daux, $url);
|
2015-06-29 16:16:39 +02:00
|
|
|
|
|
|
|
$link_props = [
|
2017-11-06 22:46:07 +01:00
|
|
|
'ri:content-title' => trim(trim($this->daux['confluence']['prefix']) . ' ' . $file->getTitle()),
|
2016-07-27 21:32:51 +02:00
|
|
|
'ri:space-key' => $this->daux['confluence']['space_id'],
|
2015-06-29 16:16:39 +02:00
|
|
|
];
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
$page = strval(new HtmlElement('ri:page', $link_props, '', true));
|
2015-10-27 16:03:06 +01:00
|
|
|
$children = $htmlRenderer->renderInlines($inline->children());
|
2016-07-27 21:32:51 +02:00
|
|
|
if (strpos($children, '<') !== false) {
|
2015-06-29 16:16:39 +02:00
|
|
|
$children = '<ac:link-body>' . $children . '</ac:link-body>';
|
|
|
|
} else {
|
|
|
|
$children = '<ac:plain-text-link-body><![CDATA[' . $children . ']]></ac:plain-text-link-body>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return new HtmlElement('ac:link', [], $page . $children);
|
|
|
|
}
|
|
|
|
}
|