You are here

public function LinkRenderer::render in Markdown 3.0.x

File

src/Plugin/Markdown/Extension/LinkRenderer.php, line 112

Class

LinkRenderer
Plugin annotation @MarkdownExtension( id = "enhanced_links", label = @Translation("Enhanced Links"), installed = TRUE, description = @Translation("Extends CommonMark to provide additional enhancements when rendering links."), parsers =…

Namespace

Drupal\markdown\Plugin\Markdown\Extension

Code

public function render(AbstractInline $inline, ElementRendererInterface $html_renderer) {
  if (!$inline instanceof Link) {
    throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
  }
  $attributes = $inline
    ->getData('attributes', []);

  // Retrieve the URL.
  $url = $inline
    ->getUrl();
  $external = $this
    ->isExternalUrl($url);
  $attributes['href'] = $url;

  // Make external links open in a new window.
  if ($this
    ->getSetting('external_new_window') && $external) {
    $attributes['target'] = '_blank';
  }

  // Add rel="nofollow".
  $no_follow = $this
    ->getSetting('no_follow');
  if ($no_follow === 'all' || $external && $no_follow === 'external' || !$external && $no_follow === 'internal') {
    $attributes['rel'] = 'nofollow';
  }
  if (isset($inline->data['title'])) {
    $attributes['title'] = Html::escape($inline->data['title']);
  }
  return new HtmlElement('a', $attributes, $html_renderer
    ->renderInlines($inline
    ->children()));
}