You are here

public function ExternalLinkRenderer::render in Markdown 8.2

File

src/Plugin/Markdown/CommonMark/Extension/ExternalLinkRenderer.php, line 34

Class

ExternalLinkRenderer

Namespace

Drupal\markdown\Plugin\Markdown\CommonMark\Extension

Code

public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) {
  if (!$inline instanceof Link) {
    throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
  }
  $attributes = $inline
    ->getData('attributes', []);
  $external = $inline
    ->getData('external');
  $attributes['href'] = $inline
    ->getUrl();
  $options = [
    'nofollow' => $this->environment
      ->getConfig('external_link/nofollow', ''),
    'noopener' => $this->environment
      ->getConfig('external_link/noopener', 'external'),
    'noreferrer' => $this->environment
      ->getConfig('external_link/noreferrer', 'external'),
  ];

  // Determine which rel attributes to set.
  $rel = [];
  foreach ($options as $type => $option) {
    switch (true) {
      case $option === 'all':
      case $external && $option === 'external':
      case !$external && $option === 'internal':
        $rel[] = $type;
    }
  }

  // Set the rel attribute.
  if ($rel) {
    $attributes['rel'] = \implode(' ', $rel);
  }
  else {
    unset($attributes['rel']);
  }
  return new HtmlElement('a', $attributes, $htmlRenderer
    ->renderInlines($inline
    ->children()));
}