xref: /plugin/commonmark/src/Dokuwiki/Plugin/Commonmark/Extension/Renderer/Inline/LinkRenderer.php (revision 12125a04766f2157bd62520100553b88a8e07727)
18ec9a8f2SSungbin Jeon<?php
28ec9a8f2SSungbin Jeon
38ec9a8f2SSungbin Jeon/*
48ec9a8f2SSungbin Jeon * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
58ec9a8f2SSungbin Jeon *
68ec9a8f2SSungbin Jeon * (c) Sungbin Jeon <clockoon@gmail.com>
78ec9a8f2SSungbin Jeon *
88ec9a8f2SSungbin Jeon * Original code based on the followings:
98ec9a8f2SSungbin Jeon * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
108ec9a8f2SSungbin Jeon * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
118ec9a8f2SSungbin Jeon *
128ec9a8f2SSungbin Jeon * For the full copyright and license information, please view the LICENSE
138ec9a8f2SSungbin Jeon * file that was distributed with this source code.
148ec9a8f2SSungbin Jeon */
158ec9a8f2SSungbin Jeon
168ec9a8f2SSungbin Jeonnamespace DokuWiki\Plugin\Commonmark\Extension\Renderer\Inline;
178ec9a8f2SSungbin Jeon
1894a075eeSSungbin Jeonuse League\CommonMark\Renderer\NodeRendererInterface;
19b0a36678SSungbin Jeonuse League\CommonMark\Renderer\ChildNodeRendererInterface;
20b0a36678SSungbin Jeonuse League\CommonMark\Node\Node;
21b0a36678SSungbin Jeonuse League\CommonMark\Extension\CommonMark\Node\Inline\Link;
22b0a36678SSungbin Jeonuse League\Config\ConfigurationAwareInterface;
23b0a36678SSungbin Jeonuse League\Config\ConfigurationInterface;
248ec9a8f2SSungbin Jeonuse League\CommonMark\Util\RegexHelper;
258ec9a8f2SSungbin Jeon
2694a075eeSSungbin Jeonfinal class LinkRenderer implements NodeRendererInterface, ConfigurationAwareInterface
278ec9a8f2SSungbin Jeon{
288ec9a8f2SSungbin Jeon    /**
298ec9a8f2SSungbin Jeon     * @var ConfigurationInterface
308ec9a8f2SSungbin Jeon     */
318ec9a8f2SSungbin Jeon    protected $config;
328ec9a8f2SSungbin Jeon
338ec9a8f2SSungbin Jeon    /**
348ec9a8f2SSungbin Jeon     * @param Link                     $inline
35b0a36678SSungbin Jeon     * @param ChildNodeRendererInterface $DWRenderer
368ec9a8f2SSungbin Jeon     *
3704312fe3SSungbin Jeon     * @return string
388ec9a8f2SSungbin Jeon     */
39b0a36678SSungbin Jeon    public function render(Node $node, ChildNodeRendererInterface $DWRenderer): string
408ec9a8f2SSungbin Jeon    {
41b0a36678SSungbin Jeon        Link::assertInstanceOf($node);
428ec9a8f2SSungbin Jeon
43b0a36678SSungbin Jeon        $attrs = $node->data->get('attributes');
448ec9a8f2SSungbin Jeon
458ec9a8f2SSungbin Jeon        $forbidUnsafeLinks = !$this->config->get('allow_unsafe_links');
46b0a36678SSungbin Jeon        if (!($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($node->getUrl()))) {
47*12125a04SSungbin Jeon            $attrs['href'] = $node->getUrl();
488ec9a8f2SSungbin Jeon        }
498ec9a8f2SSungbin Jeon
50b0a36678SSungbin Jeon        $result = '[[' . $attrs['href'] . '|' . $DWRenderer->renderNodes($node->children()) . ']]';
518ec9a8f2SSungbin Jeon        return $result;
528ec9a8f2SSungbin Jeon    }
538ec9a8f2SSungbin Jeon
54b0a36678SSungbin Jeon    public function setConfiguration(ConfigurationInterface $configuration): void
558ec9a8f2SSungbin Jeon    {
568ec9a8f2SSungbin Jeon        $this->config = $configuration;
578ec9a8f2SSungbin Jeon    }
588ec9a8f2SSungbin Jeon}
59