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;
|
2016-08-16 23:25:06 +02:00
|
|
|
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;
|
|
|
|
|
2019-11-28 23:32:33 +01:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-04 20:33:44 +02:00
|
|
|
* @param AbstractInline|Link $inline
|
2020-04-22 22:24:52 +02:00
|
|
|
*
|
2016-08-16 23:25:06 +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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2016-08-16 23:25:06 +02:00
|
|
|
// empty urls and anchors should
|
|
|
|
// not go through the url resolver
|
2019-11-30 20:40:42 +01:00
|
|
|
if (!DauxHelper::isValidUrl($url)) {
|
2016-07-04 20:33:44 +02:00
|
|
|
return $element;
|
2015-06-29 16:16:39 +02:00
|
|
|
}
|
|
|
|
|
2016-08-16 23:25:06 +02:00
|
|
|
// Absolute urls, shouldn't either
|
2019-11-30 20:40:42 +01:00
|
|
|
if (DauxHelper::isExternalUrl($url)) {
|
2018-02-25 23:02:09 +01:00
|
|
|
$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
|
|
|
|
2016-08-16 23:25:06 +02:00
|
|
|
return $element;
|
|
|
|
}
|
2016-07-04 20:33:44 +02:00
|
|
|
|
2016-08-25 23:15:15 +02:00
|
|
|
// 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);
|
2016-08-25 23:15:15 +02:00
|
|
|
$url = $urlAndHash[0];
|
|
|
|
|
2019-05-28 21:26:27 +02:00
|
|
|
$foundWithHash = false;
|
|
|
|
|
2016-08-16 23:25:06 +02:00
|
|
|
try {
|
2019-11-30 20:40:42 +01:00
|
|
|
$file = DauxHelper::resolveInternalFile($this->daux, $url);
|
2016-08-16 23:25:06 +02:00
|
|
|
$url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl());
|
2016-09-21 21:49:55 +02:00
|
|
|
} catch (LinkNotFoundException $e) {
|
2019-05-28 21:26:27 +02:00
|
|
|
// 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) {
|
2019-11-30 20:40:42 +01:00
|
|
|
$file = DauxHelper::resolveInternalFile($this->daux, $url . '#' . $urlAndHash[1]);
|
2019-05-28 21:26:27 +02:00
|
|
|
$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
|
2019-05-28 21:26:27 +02:00
|
|
|
// one will tell the same.
|
2016-08-16 23:25:06 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 21:26:27 +02:00
|
|
|
if (!$foundWithHash) {
|
|
|
|
if ($this->daux->isStatic()) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2020-04-22 21:39:15 +02:00
|
|
|
|
2019-05-28 21:26:27 +02:00
|
|
|
$element->setAttribute('class', 'Link--broken');
|
|
|
|
}
|
2016-08-16 23:25:06 +02:00
|
|
|
}
|
2016-07-04 20:33:44 +02:00
|
|
|
|
2019-05-28 21:26:27 +02:00
|
|
|
if (!$foundWithHash && isset($urlAndHash[1])) {
|
2016-09-26 20:54:06 +02:00
|
|
|
$url .= '#' . $urlAndHash[1];
|
2016-08-25 23:15:15 +02:00
|
|
|
}
|
|
|
|
|
2016-07-04 20:33:44 +02:00
|
|
|
$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
|
|
|
}
|