1<?php 2 3/* 4 * This file is part of the clockoon/dokuwiki-commonmark-plugin package. 5 * 6 * (c) Sungbin Jeon <clockoon@gmail.com> 7 * 8 * Original code based on the followings: 9 * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane 10 * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com> 11 * 12 * For the full copyright and license information, please view the LICENSE 13 * file that was distributed with this source code. 14 */ 15 16namespace DokuWiki\Plugin\Commonmark\Extension\Renderer\Inline; 17 18use League\CommonMark\Renderer\NodeRendererInterface; 19use League\CommonMark\Renderer\ChildNodeRendererInterface; 20use League\CommonMark\Node\Node; 21use League\CommonMark\Extension\CommonMark\Node\Inline\Link; 22use League\Config\ConfigurationAwareInterface; 23use League\Config\ConfigurationInterface; 24use League\CommonMark\Util\RegexHelper; 25 26final class LinkRenderer implements NodeRendererInterface, ConfigurationAwareInterface 27{ 28 /** 29 * @var ConfigurationInterface 30 */ 31 protected $config; 32 33 /** 34 * @param Link $inline 35 * @param ChildNodeRendererInterface $DWRenderer 36 * 37 * @return string 38 */ 39 public function render(Node $node, ChildNodeRendererInterface $DWRenderer): string 40 { 41 Link::assertInstanceOf($node); 42 43 $attrs = $node->data->get('attributes'); 44 45 $forbidUnsafeLinks = !$this->config->get('allow_unsafe_links'); 46 if (!($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($node->getUrl()))) { 47 $attrs['href'] = $node->getUrl(); 48 } 49 50 $result = '[[' . $attrs['href'] . '|' . $DWRenderer->renderNodes($node->children()) . ']]'; 51 return $result; 52 } 53 54 public function setConfiguration(ConfigurationInterface $configuration): void 55 { 56 $this->config = $configuration; 57 } 58} 59