public function ReadmehelpMarkdown::convertMarkdownImage in README Help 8
Converts markdown syntax image into HTML image.
The markdown image should look like this: @code ## A relative to a module directory path. The "Title" is optional. ![Alt](images/my-image.png "Title")
## An absolute path ![Alt]( https://example.com/modules/contrib/my_module/images/my-image.png "Title") @code
Note that in the case of relative path the internal path to a module's directory is taken from the current request. So, if the request looks like https://example.com/modules/contrib/my_module/images then the markdown image should be constructed like this: @code ![Alt](my-image.png "Title") @code
Parameters
string $text: The string to be filtered.
string $host: (optional) The base URL of a site, like: http(s)://example.com.
string $path: (optional) The path part of the URL, like: modules/contrib/my_module.
Return value
string The HTML source with image tags.
1 call to ReadmehelpMarkdown::convertMarkdownImage()
- ReadmehelpMarkdown::process in src/
Plugin/ Filter/ ReadmehelpMarkdown.php - Performs the filter processing.
File
- src/
Plugin/ Filter/ ReadmehelpMarkdown.php, line 565
Class
- ReadmehelpMarkdown
- Provides a filter for markdown.
Namespace
Drupal\readmehelp\Plugin\FilterCode
public function convertMarkdownImage($text, $host, $path) {
$parts = explode('?', $path);
$path = isset($parts[0]) ? $parts[0] : 'NOT-A-PATH';
$pattern = '/(!\\[((?>[^\\[\\]]+|\\[\\])*)\\]\\s?\\([ \\n]*(?:<(\\S*)>|((?>[^()\\s]+|\\((?>\\)))*))[ \\n]*(([\'"])(.*?)\\6[ \\n]*)?\\))/xs';
return preg_replace_callback($pattern, function ($matches) use ($host, $path) {
$alt = $matches[2];
$url = $matches[3] == '' ? $matches[4] : $matches[3];
$title = empty($matches[7]) ? $alt : $matches[7];
$src = "{$host}/{$path}/{$url}";
if (preg_match('/^http/', $url)) {
$src = $url;
}
return "<img src=\"{$src}\" alt=\"{$alt}\" title=\"{$title}\" class=\"markdown-image\" />";
}, $text);
}