From: Vincent Vanwaelscappel Date: Wed, 17 Jul 2019 13:38:46 +0000 (+0200) Subject: wip #2893 @0:15 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=e770846b919f65529b1a6f2eb055e964bd857105;p=cubist_cms-back.git wip #2893 @0:15 --- diff --git a/src/app/Magic/Menu/Menu.php b/src/app/Magic/Menu/Menu.php index 6d7f68a..3a5d419 100644 --- a/src/app/Magic/Menu/Menu.php +++ b/src/app/Magic/Menu/Menu.php @@ -38,6 +38,26 @@ class Menu extends BaseMenu } + public static function internalToHref($url) + { + if (stristr($url, 'internal:')) { + $e = explode(':', $url, 2); + $url = $e[1]; + } + + if (is_numeric($url)) { + $searchParam = 'id'; + } else { + $searchParam = 'name'; + } + + $item = self::getNavigation()->findOneBy($searchParam, $url); + if (null !== $item) { + return $item->getHref(); + } + return '#internalnotfound'; + } + public function makeAllMenus() { $nav = self::getNavigation(); diff --git a/src/app/Markdown/InternaLink/Extension.php b/src/app/Markdown/InternaLink/Extension.php new file mode 100644 index 0000000..65ef941 --- /dev/null +++ b/src/app/Markdown/InternaLink/Extension.php @@ -0,0 +1,17 @@ +addInlineRenderer(Link::class, new Renderer(), 10); + } +} diff --git a/src/app/Markdown/InternaLink/Renderer.php b/src/app/Markdown/InternaLink/Renderer.php new file mode 100644 index 0000000..5db2310 --- /dev/null +++ b/src/app/Markdown/InternaLink/Renderer.php @@ -0,0 +1,64 @@ +getData('attributes', []); + + $forbidUnsafeLinks = !$this->config->get('allow_unsafe_links'); + if (!($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl()))) { + $href = $inline->getUrl(); + if (stristr($href, 'internal:')) { + $href = Menu::internalToHref($href); + } + $attrs['href'] = $href; + } + + if (isset($inline->data['title'])) { + $attrs['title'] = $inline->data['title']; + } + + if (isset($attrs['target']) && $attrs['target'] === '_blank' && !isset($attrs['rel'])) { + $attrs['rel'] = 'noopener noreferrer'; + } + + return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->children())); + } + + /** + * @param ConfigurationInterface $configuration + */ + public function setConfiguration(ConfigurationInterface $configuration) + { + $this->config = $configuration; + } +}