daux.io/libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php

55 regels
2.0 KiB
PHP

<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
2015-06-29 16:16:39 +02:00
use League\CommonMark\HtmlElement;
use League\CommonMark\HtmlRendererInterface;
use League\CommonMark\Inline\Element\AbstractInline;
use League\CommonMark\Inline\Element\Link;
class LinkRenderer extends \Todaymade\Daux\Format\Base\ContentTypes\Markdown\LinkRenderer
2015-06-29 16:16:39 +02:00
{
/**
* @param Link $inline
* @param HtmlRendererInterface $htmlRenderer
*
* @return HtmlElement
*/
public function render(AbstractInline $inline, HtmlRendererInterface $htmlRenderer)
{
2015-07-20 15:59:52 +02:00
// This can't be in the method type as
// the method is an abstract and should
// have the same interface
if (!$inline instanceof Link) {
throw new \RuntimeException(
"Wrong type passed to " . __CLASS__ . "::" . __METHOD__ .
" the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" .
get_class($inline) . "' was provided"
);
}
2015-06-29 16:16:39 +02:00
// Default handling
$element = parent::render($inline, $htmlRenderer);
$url = $inline->getUrl();
if (empty($url) || $url[0] != '!') {
return $element;
}
//Internal links
$file = $this->resolveInternalFile(ltrim($url, "!"));
$link_props = [
'ri:content-title' => trim($this->daux['confluence']['prefix']) . " " . $file->getTitle(),
'ri:space-key' => $this->daux['confluence']['space_id']
];
2015-07-17 23:38:06 +02:00
$page = strval(new HtmlElement('ri:page', $link_props, '', true));
2015-06-29 16:16:39 +02:00
$children = $htmlRenderer->renderInlines($inline->getChildren());
if (strpos($children, "<") !== false) {
$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);
}
}