Added support for absolute path images and refactored a little

This commit is contained in:
Andy Miller
2014-08-30 14:19:14 -06:00
parent a33bfd8cb0
commit f95d6b11de

View File

@@ -16,10 +16,7 @@ trait MarkdownGravLinkTrait
// Run the parent method to get the actual results
$Excerpt = parent::identifyLink($Excerpt);
$actions = array();
$command = '';
$config = self::$grav['config'];
$base_url = trim($config->get('system.base_url_relative'));
$base_url_full = trim($config->get('system.base_url_absolute'));
$this->base_url = trim(self::$grav['config']->get('system.base_url_relative'));
// if this is a link
if (isset($Excerpt['element']['attributes']['href'])) {
@@ -29,51 +26,8 @@ trait MarkdownGravLinkTrait
// if there is no host set but there is a path, the file is local
if (!isset($url['host']) && isset($url['path'])) {
$markdown_url = $url['path'];
$not_relative_urls = ['/','http://','https://'];
$valid = true;
// make sure the url is relative
foreach ($not_relative_urls as $needle) {
if (strpos($markdown_url, $needle) === 0) {
$valid = false;
break;
}
}
// if it is a valid relative url being the transformation
if ($valid) {
$relative_path = rtrim($base_url, '/') . $this->page->route();
// If this is a 'real' filepath clean it up
if (file_exists($this->page->path().'/'.$markdown_url)) {
$relative_path = rtrim($base_url, '/') .
preg_replace('/\/([\d]+.)/', '/',
str_replace(PAGES_DIR, '/', $this->page->path()));
$markdown_url = preg_replace('/^([\d]+.)/', '',
preg_replace('/\/([\d]+.)/', '/', $markdown_url));
}
// else its a relative path already
$newpath = array();
$paths = explode('/', $markdown_url);
// remove the updirectory references (..)
foreach ($paths as $path) {
if ($path == '..') {
$relative_path = dirname($relative_path);
} else {
$newpath[] = $path;
}
}
// build the new url
$new_url = $relative_path . '/' . implode('/', $newpath);
// set the new url back on the Excerpt
$Excerpt['element']['attributes']['href'] = $new_url;
}
// convert the URl is required
$Excerpt['element']['attributes']['href'] = $this->convertUrl($url['path']);
}
}
@@ -136,9 +90,57 @@ trait MarkdownGravLinkTrait
// Set the lightbox element on the Excerpt
$Excerpt['element'] = $Element;
}
} else {
// not a current page media file, see if it needs converting to relative
$Excerpt['element']['attributes']['src'] = $this->convertUrl($url['path']);
}
}
}
return $Excerpt;
}
/**
* Converts links from absolute '/' or relative (../..) to a grav friendly format
* @param string $markdown_url the URL as it was written in the markdown
* @return string the more friendly formatted url
*/
protected function convertUrl($markdown_url)
{
// if absolue and starts with a base_url move on
if (strpos($markdown_url, $this->base_url) === 0) {
$new_url = $markdown_url;
// if its absolute with /
} elseif (strpos($markdown_url, '/') === 0) {
$new_url = rtrim($this->base_url, '/') . $markdown_url;
} else {
$relative_path = rtrim($this->base_url, '/') . $this->page->route();
// If this is a 'real' filepath clean it up
if (file_exists($this->page->path().'/'.$markdown_url)) {
$relative_path = rtrim($this->base_url, '/') .
preg_replace('/\/([\d]+.)/', '/',
str_replace(PAGES_DIR, '/', $this->page->path()));
$markdown_url = preg_replace('/^([\d]+.)/', '',
preg_replace('/\/([\d]+.)/', '/', $markdown_url));
}
// else its a relative path already
$newpath = array();
$paths = explode('/', $markdown_url);
// remove the updirectory references (..)
foreach ($paths as $path) {
if ($path == '..') {
$relative_path = dirname($relative_path);
} else {
$newpath[] = $path;
}
}
// build the new url
$new_url = $relative_path . '/' . implode('/', $newpath);
}
return $new_url;
}
}