From 4cd4bb4202e7dd23d7752f2688ee77d8a818f3db Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Tue, 20 Jan 2015 09:35:30 -0700 Subject: [PATCH] fix for non-url valid twig code in Markdown Link/Image --- .../Common/Markdown/MarkdownGravLinkTrait.php | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/system/src/Grav/Common/Markdown/MarkdownGravLinkTrait.php b/system/src/Grav/Common/Markdown/MarkdownGravLinkTrait.php index 94e407f9c..e2c8e3631 100644 --- a/system/src/Grav/Common/Markdown/MarkdownGravLinkTrait.php +++ b/system/src/Grav/Common/Markdown/MarkdownGravLinkTrait.php @@ -17,6 +17,8 @@ trait MarkdownGravLinkTrait protected $base_url; protected $pages_dir; + protected $twig_link_regex = '/\!*\[(?:.*)\]\(([{{|{%|{#].*[#}|%}|}}])\)/'; + /** * Initialiazation function to setup key variables needed by the MarkdownGravLinkTrait * @@ -45,8 +47,16 @@ trait MarkdownGravLinkTrait protected function inlineImage($excerpt) { - // Run the parent method to get the actual results - $excerpt = parent::inlineImage($excerpt); + if (preg_match($this->twig_link_regex, $excerpt['text'], $matches)) { + $excerpt['text'] = str_replace($matches[1], '/', $excerpt['text']); + $excerpt = parent::inlineImage($excerpt); + $excerpt['element']['attributes']['src'] = $matches[1]; + $excerpt['extent'] = $excerpt['extent'] + strlen($matches[1]) - 1; + return $excerpt; + } else { + $excerpt = parent::inlineImage($excerpt); + } + $actions = array(); // if this is an image @@ -131,8 +141,16 @@ trait MarkdownGravLinkTrait protected function inlineLink($excerpt) { - // Run the parent method to get the actual results - $excerpt = parent::inlineLink($excerpt); + // do some trickery to get around Parsedown requirement for valid URL if its Twig in there + if (preg_match($this->twig_link_regex, $excerpt['text'], $matches)) { + $excerpt['text'] = str_replace($matches[1], '/', $excerpt['text']); + $excerpt = parent::inlineLink($excerpt); + $excerpt['element']['attributes']['href'] = $matches[1]; + $excerpt['extent'] = $excerpt['extent'] + strlen($matches[1]) - 1; + return $excerpt; + } else { + $excerpt = parent::inlineLink($excerpt); + } // if this is a link if (isset($excerpt['element']['attributes']['href'])) {