You are here

protected function HashAutolinker::getUrlTitle in Markdown 3.0.x

Retrieves a URL page title.

Parameters

string $url: The URL to retrieve the title from.

Return value

string|false The URL title or FALSE if it could not be retrieved.

1 call to HashAutolinker::getUrlTitle()
HashAutolinker::parse in src/Plugin/Markdown/Extension/HashAutolinker.php

File

src/Plugin/Markdown/Extension/HashAutolinker.php, line 150

Class

HashAutolinker
Plugin annotation @MarkdownExtension( id = "hash_autolinker", label = @Translation("# Autolinker"), installed = TRUE, description = @Translation("Automatically link commonly used references that come after a hash character (#) without having…

Namespace

Drupal\markdown\Plugin\Markdown\Extension

Code

protected function getUrlTitle($url) {
  $response = \Drupal::httpClient()
    ->get($url);
  if ($response
    ->getStatusCode() >= 200 && $response
    ->getStatusCode() < 400) {
    $dom = new \DOMDocument();
    @$dom
      ->loadHTML($response
      ->getBody()
      ->getContents());
    if (($title = $dom
      ->getElementsByTagName('title')) && $title->length) {
      return Html::escape(trim(preg_replace('/\\s+/', ' ', $title
        ->item(0)->textContent)));
    }
  }
  return FALSE;
}