Skip to content

Commit

Permalink
Fixed twig parsing errors in pages where twig is parsed after markdown [
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Jan 20, 2021
1 parent 440fa26 commit a0c3140
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Fixed `bin/gpm selfupgrade` failing to report failed Grav update [#3116](https://github.com/getgrav/grav/issues/3116)
* Fixed `bin/gpm selfupgrade` error on `Call to undefined method` [#3160](https://github.com/getgrav/grav/issues/3160)
* Flex Pages: Fixed fatal error when trying to move a page to Root (/) [#3161](https://github.com/getgrav/grav/issues/3161)
* Fixed twig parsing errors in pages where twig is parsed after markdown [#3162](https://github.com/getgrav/grav/issues/3162)

# v1.7.0
## 01/19/2021
Expand Down
27 changes: 24 additions & 3 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ public function content($var = null)
Grav::instance()->fireEvent('onPageContentProcessed', new Event(['page' => $this]));
} else {
if ($process_markdown) {
$this->processMarkdown();
$this->processMarkdown($process_twig);
}

// Content Processed but not cached yet
Expand Down Expand Up @@ -871,9 +871,10 @@ public function setContentMeta($content_meta)
/**
* Process the Markdown content. Uses Parsedown or Parsedown Extra depending on configuration
*
* @param bool $keepTwig If true, content between twig tags will not be processed.
* @return void
*/
protected function processMarkdown()
protected function processMarkdown(bool $keepTwig = false)
{
/** @var Config $config */
$config = Grav::instance()['config'];
Expand Down Expand Up @@ -905,7 +906,27 @@ protected function processMarkdown()
$parsedown = new Parsedown($excerpts);
}

$this->content = $parsedown->text($this->content);
$content = $this->content;
if ($keepTwig) {
// Base64 encode any twig.
$content = preg_replace_callback(
['/({#)(.*?)(#})/mu', '/({{)(.*?)(}})/mu', '/({%)(.*?)(%})/mu'],
static function ($matches) { return $matches[1] . base64_encode($matches[2]) . $matches[3]; },
$content
);
}
$content = $parsedown->text($content);

if ($keepTwig) {
// Base64 decode the encoded twig.
$content = preg_replace_callback(
['/({#)(.*?)(#})/mu', '/({{)(.*?)(}})/mu', '/({%)(.*?)(%})/mu'],
static function ($matches) { return $matches[1] . base64_decode($matches[2]) . $matches[3]; },
$content
);
}

$this->content = $content;
}


Expand Down
24 changes: 23 additions & 1 deletion system/src/Grav/Framework/Flex/Pages/Traits/PageContentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ protected function processContent($content): string
$grav->fireEvent('onPageContentProcessed', new Event(['page' => $this]));
} else {
if ($process_markdown) {
$options['keep_twig'] = $process_twig;
$this->_content = $this->processMarkdown($this->_content, $options);
}

Expand Down Expand Up @@ -807,7 +808,28 @@ protected function processMarkdown($content, array $options = []): string
$parsedown = new Parsedown($excerpts);
}

return $parsedown->text($content);
$keepTwig = (bool)($options['keep_twig'] ?? false);
if ($keepTwig) {
// Base64 encode any twig.
$content = preg_replace_callback(
['/({#)(.*?)(#})/mu', '/({{)(.*?)(}})/mu', '/({%)(.*?)(%})/mu'],
static function ($matches) { return $matches[1] . base64_encode($matches[2]) . $matches[3]; },
$content
);
}

$content = $parsedown->text($content);

if ($keepTwig) {
// Base64 decode the encoded twig.
$content = preg_replace_callback(
['/({#)(.*?)(#})/mu', '/({{)(.*?)(}})/mu', '/({%)(.*?)(%})/mu'],
static function ($matches) { return $matches[1] . base64_decode($matches[2]) . $matches[3]; },
$content
);
}

return $content;
}

abstract protected function loadHeaderProperty(string $property, $var, callable $filter);
Expand Down

0 comments on commit a0c3140

Please sign in to comment.