You are here

public function ReadmehelpMarkdown::convertMarkdownLink in README Help 8

Converts markdown syntax anchor into HTML anchor.

The markdown anchor should look like this: @code > The Drupal site's internal page path. The "Title" is optional. The url is taken from the link's textual part. The # part of the url is to render README markdown file and have the same link to look similar both on the Drupal site and extenal vcs systems like Github. The difference is that on Github it works just as a dummy link but on the Drupal site it leads to a really existing page. [admin/reports/dblog](#admin-reports-dblog "Title")

## An absolute path. [Recent log messages](https://example.com/admin/reports/dblog "Title") @code

Parameters

string $text: The string to be filtered.

string $host: (optional) The base URL of a site, like: http(s)://example.com.

Return value

string The HTML source with anchor tags.

1 call to ReadmehelpMarkdown::convertMarkdownLink()
ReadmehelpMarkdown::process in src/Plugin/Filter/ReadmehelpMarkdown.php
Performs the filter processing.

File

src/Plugin/Filter/ReadmehelpMarkdown.php, line 607

Class

ReadmehelpMarkdown
Provides a filter for markdown.

Namespace

Drupal\readmehelp\Plugin\Filter

Code

public function convertMarkdownLink($text, $host) {
  $pattern = '/(\\[((?>[^\\[\\]]+|\\[\\])*)\\]\\([ \\n]*(?:<(.+?)>|((?>[^()\\s]+|\\((?>\\)))*))[ \\n]*(([\'"])(.*?)\\6[ \\n]*)?\\))/xs';
  return preg_replace_callback($pattern, function ($matches) use ($host) {
    $text = $matches[2];
    $parts = explode('?', $text);
    $text = isset($parts[0]) ? $parts[0] : 'NOT-A-TEXT';
    $url = $matches[3] == '' ? $matches[4] : $matches[3];
    $parts = explode('?', $url);
    $url = isset($parts[0]) ? $parts[0] : 'NOT-A-URL';
    $title = empty($matches[7]) ? $text : $matches[7];
    if (preg_match('{^.*[#].*}', $text)) {
      $parts = explode('?', $text);
      $url = "{$host}/{$parts[0]}";
      $text = $title;
    }
    elseif (!preg_match('/^http/', $url)) {
      $url = "{$host}/{$text}";
    }
    return "<a href=\"{$url}\" title=\"{$title}\" class=\"markdown-link\">{$text}</a>";
  }, $text);
}