}
+ 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();
--- /dev/null
+<?php
+
+namespace Cubist\Backpack\app\Markdown\InternalLink;
+
+
+use League\CommonMark\Block\Element\Heading;
+use League\CommonMark\ConfigurableEnvironmentInterface;
+use League\CommonMark\Extension\ExtensionInterface;
+use League\CommonMark\Inline\Element\Link;
+
+class Extension implements ExtensionInterface
+{
+ public function register(ConfigurableEnvironmentInterface $environment)
+ {
+ $environment->addInlineRenderer(Link::class, new Renderer(), 10);
+ }
+}
--- /dev/null
+<?php
+
+
+namespace Cubist\Backpack\app\Markdown\InternalLink;
+
+
+use Cubist\Backpack\app\Magic\Menu\Menu;
+use League\CommonMark\ElementRendererInterface;
+use League\CommonMark\HtmlElement;
+use League\CommonMark\Inline\Element\AbstractInline;
+use League\CommonMark\Inline\Element\Link;
+use League\CommonMark\Inline\Renderer\InlineRendererInterface;
+use League\CommonMark\Util\ConfigurationInterface;
+use League\CommonMark\Util\RegexHelper;
+
+class Renderer implements InlineRendererInterface
+{
+ /**
+ * @var ConfigurationInterface
+ */
+ protected $config;
+
+ /**
+ * @param Link $inline
+ * @param ElementRendererInterface $htmlRenderer
+ *
+ * @return HtmlElement
+ */
+ public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
+ {
+ if (!($inline instanceof Link)) {
+ throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
+ }
+
+ $attrs = $inline->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;
+ }
+}