From e801c8f44e02f6fcea9e5cb8e513b69982e08f45 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Tue, 19 Jan 2016 15:48:18 -0700 Subject: [PATCH] Added support for `absolute_urls: true` and page level ssl --- .../Common/Markdown/ParsedownGravTrait.php | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/system/src/Grav/Common/Markdown/ParsedownGravTrait.php b/system/src/Grav/Common/Markdown/ParsedownGravTrait.php index 6617ab5a0..f65478573 100644 --- a/system/src/Grav/Common/Markdown/ParsedownGravTrait.php +++ b/system/src/Grav/Common/Markdown/ParsedownGravTrait.php @@ -320,12 +320,63 @@ trait ParsedownGravTrait unset ($url['query']); } + // set path to / if not set + if (empty($url['path'])) { + $url['path'] = '/'; + } + // if there is no scheme, the file is local and we'll need to convert that URL if (!isset($url['scheme']) && (count($url) > 0)) { - $excerpt['element']['attributes']['href'] = Uri::convertUrl($this->page, Uri::buildUrl($url), $type, true); - } else { - $excerpt['element']['attributes']['href'] = Uri::buildUrl($url); + $url['path'] = Uri::convertUrl($this->page, $url['path'], $type, true); } + + // if absolute urls enabled, add them + if (self::getGrav()['config']->get('system.absolute_urls', false)) { + $uri = self::getGrav()['uri']; + $url['scheme'] = str_replace('://', '', $uri->scheme()); + $url['host'] = $uri->host(); + + if ($uri->port() != 80 && $uri->port() != 443) { + $url['port'] = $uri->port(); + } + + // check if page exists for this route, and if so, check if it has SSL enabled + $pages = self::getGrav()['pages']; + $routes = $pages->routes(); + + // if this is an image, get the proper path + $url_bits = pathinfo($url['path']); + if (isset($url_bits['extension'])) { + $target_path = $url_bits['dirname']; + } else { + $target_path = $url['path']; + } + + // strip base from this path + $target_path = str_replace($uri->rootUrl(), '', $target_path); + + // set to / if root + if (empty($target_path)) { + $target_path = '/'; + } + + // look to see if this page exists and has ssl enabled + if (isset($routes[$target_path])) { + $target_page = $pages->get($routes[$target_path]); + if ($target_page) { + $ssl_enabled = $target_page->ssl(); + if (isset($ssl_enabled)) { + if ($ssl_enabled) { + $url['scheme'] = 'https'; + } else { + $url['scheme'] = 'http'; + } + } + } + } + } + + $excerpt['element']['attributes']['href'] = Uri::buildUrl($url); } return $excerpt;