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

110 lines
3.5 KiB
PHP
Raw Normal View History

2015-07-29 22:31:41 +02:00
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
2015-06-29 16:16:39 +02:00
2015-10-21 23:26:42 +02:00
use League\CommonMark\ElementRendererInterface;
2016-07-29 23:46:57 +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-28 22:36:26 +01:00
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
use League\CommonMark\Util\ConfigurationAwareInterface;
use League\CommonMark\Util\ConfigurationInterface;
2015-06-29 16:16:39 +02:00
use Todaymade\Daux\Config;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception\LinkNotFoundException;
2015-06-29 16:16:39 +02:00
2019-11-28 22:36:26 +01:00
class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterface
2015-06-29 16:16:39 +02:00
{
/**
* @var Config
*/
protected $daux;
/**
* @var \League\CommonMark\Inline\Renderer\LinkRenderer
*/
protected $parent;
2015-06-29 16:16:39 +02:00
public function __construct($daux)
{
$this->daux = $daux;
2019-11-28 22:41:53 +01:00
$this->parent = new \League\CommonMark\Inline\Renderer\LinkRenderer();
2015-06-29 16:16:39 +02:00
}
/**
* @param AbstractInline|Link $inline
2020-04-22 22:24:52 +02:00
*
* @throws LinkNotFoundException
2020-04-22 22:24:52 +02:00
*
* @return HtmlElement
2015-06-29 16:16:39 +02:00
*/
2015-10-21 23:26:42 +02:00
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
2015-06-29 16:16:39 +02:00
{
if (!($inline instanceof Link)) {
throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
2015-07-20 15:59:52 +02:00
}
2019-11-28 22:36:26 +01:00
$element = $this->parent->render($inline, $htmlRenderer);
2015-06-29 16:16:39 +02:00
$url = $inline->getUrl();
// empty urls and anchors should
// not go through the url resolver
if (!DauxHelper::isValidUrl($url)) {
return $element;
2015-06-29 16:16:39 +02:00
}
// Absolute urls, shouldn't either
if (DauxHelper::isExternalUrl($url)) {
$element->setAttribute('class', 'Link--external');
2020-04-22 21:39:15 +02:00
$element->setAttribute('rel', 'noopener noreferrer');
2016-09-26 20:54:06 +02:00
return $element;
}
// if there's a hash component in the url, ensure we
// don't put that part through the resolver.
2016-09-26 20:54:06 +02:00
$urlAndHash = explode('#', $url);
$url = $urlAndHash[0];
$foundWithHash = false;
try {
$file = DauxHelper::resolveInternalFile($this->daux, $url);
$url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl());
} catch (LinkNotFoundException $e) {
// For some reason, the filename could contain a # and thus the link needs to resolve to that.
try {
2020-04-22 22:24:52 +02:00
if (strlen($urlAndHash[1] ?? '') > 0) {
$file = DauxHelper::resolveInternalFile($this->daux, $url . '#' . $urlAndHash[1]);
$url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl());
$foundWithHash = true;
}
} catch (LinkNotFoundException $e2) {
2020-04-22 21:39:15 +02:00
// If it's still not found here, we'll only
// report on the first error as the second
// one will tell the same.
}
if (!$foundWithHash) {
if ($this->daux->isStatic()) {
throw $e;
}
2020-04-22 21:39:15 +02:00
$element->setAttribute('class', 'Link--broken');
}
}
if (!$foundWithHash && isset($urlAndHash[1])) {
2016-09-26 20:54:06 +02:00
$url .= '#' . $urlAndHash[1];
}
$element->setAttribute('href', $url);
2015-06-29 16:16:39 +02:00
return $element;
}
2019-11-28 22:36:26 +01:00
public function setConfiguration(ConfigurationInterface $configuration)
{
$this->parent->setConfiguration($configuration);
}
2015-06-29 16:16:39 +02:00
}