2018-09-21 23:43:06 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\HTMLFile\ContentTypes\Markdown;
|
|
|
|
|
|
|
|
use League\CommonMark\ElementRendererInterface;
|
|
|
|
use League\CommonMark\HtmlElement;
|
|
|
|
use League\CommonMark\Inline\Element\AbstractInline;
|
|
|
|
use League\CommonMark\Inline\Element\Link;
|
|
|
|
use Todaymade\Daux\Config;
|
|
|
|
use Todaymade\Daux\DauxHelper;
|
|
|
|
use Todaymade\Daux\Exception\LinkNotFoundException;
|
|
|
|
use Todaymade\Daux\Tree\Entry;
|
|
|
|
|
|
|
|
class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param AbstractInline|Link $inline
|
|
|
|
* @param ElementRendererInterface $htmlRenderer
|
|
|
|
* @return HtmlElement
|
|
|
|
* @throws LinkNotFoundException
|
|
|
|
*/
|
|
|
|
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
|
|
|
|
{
|
2019-11-30 22:24:10 +01:00
|
|
|
if (!($inline instanceof Link)) {
|
|
|
|
throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
|
2018-09-21 23:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$element = parent::render($inline, $htmlRenderer);
|
|
|
|
|
|
|
|
$url = $inline->getUrl();
|
|
|
|
|
|
|
|
// empty urls and anchors should
|
|
|
|
// not go through the url resolver
|
2019-11-30 22:24:10 +01:00
|
|
|
if (!DauxHelper::isValidUrl($url)) {
|
2018-09-21 23:43:06 +02:00
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Absolute urls, shouldn't either
|
2019-11-30 22:24:10 +01:00
|
|
|
if (DauxHelper::isExternalUrl($url)) {
|
2018-09-21 23:43:06 +02:00
|
|
|
$element->setAttribute('class', 'Link--external');
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there's a hash component in the url, we can directly use it as all pages are in the same file
|
|
|
|
$urlAndHash = explode('#', $url);
|
|
|
|
if (isset($urlAndHash[1])) {
|
|
|
|
$element->setAttribute('href', '#' . $urlAndHash[1]);
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-11-30 20:40:42 +01:00
|
|
|
$file = DauxHelper::resolveInternalFile($this->daux, $url);
|
2018-09-21 23:43:06 +02:00
|
|
|
$url = $file->getUrl();
|
|
|
|
} catch (LinkNotFoundException $e) {
|
|
|
|
if ($this->daux->isStatic()) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$element->setAttribute('class', 'Link--broken');
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = str_replace('/', '_', $url);
|
|
|
|
$element->setAttribute('href', "#file_$url");
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
}
|