From 2c1f7664abe6d23a8f0918e2ec01583e718e6aa9 Mon Sep 17 00:00:00 2001 From: Steven Spungin Date: Mon, 5 Feb 2018 13:13:40 -0500 Subject: [PATCH 1/3] Add pages.markdown.extra_escape_fences feature --- system/blueprints/config/system.yaml | 10 +++++++++ system/src/Grav/Common/Page/Page.php | 18 ++++++++++++++-- .../Grav/Common/Page/ParsedownExtraUtil.php | 21 +++++++++++++++++++ user/config/system.yaml | 1 + 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 system/src/Grav/Common/Page/ParsedownExtraUtil.php diff --git a/system/blueprints/config/system.yaml b/system/blueprints/config/system.yaml index d5e5cccf6d..7efb8a7051 100644 --- a/system/blueprints/config/system.yaml +++ b/system/blueprints/config/system.yaml @@ -430,6 +430,16 @@ form: 0: PLUGIN_ADMIN.NO validate: type: bool + pages.markdown.extra_escape_fences: + type: toggle + label: Escape HTML elements in markdown extra fences + help: PLUGIN_ADMIN.MARKDOWN_EXTRA_ESCAPE_FENCES_HELP + highlight: 1 + options: + 1: PLUGIN_ADMIN.YES + 0: PLUGIN_ADMIN.NO + validate: + type: bool pages.markdown.auto_line_breaks: type: toggle label: PLUGIN_ADMIN.AUTO_LINE_BREAKS diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index fd9b04d89a..328ea19ad3 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -766,14 +766,28 @@ protected function processMarkdown() $defaults['extra'] = $this->markdown_extra ?: $config->get('system.pages.markdown_extra'); } + $content = $this->content; + $needToUnescape = false; // Initialize the preferred variant of Parsedown if ($defaults['extra']) { $parsedown = new ParsedownExtra($this, $defaults); - } else { + if ($config->get("system.pages.markdown.extra_escape_fences")) { + include_once "ParsedownExtraUtil.php"; + $content = escapeFences($content); + $needToUnescape = true; + } + } + else { $parsedown = new Parsedown($this, $defaults); } - $this->content = $parsedown->text($this->content); + $content = $parsedown->text($content); + + if ($needToUnescape) { + $content = unescapeFences($content); + } + + $this->content = $content; } diff --git a/system/src/Grav/Common/Page/ParsedownExtraUtil.php b/system/src/Grav/Common/Page/ParsedownExtraUtil.php new file mode 100644 index 0000000000..5812d13065 --- /dev/null +++ b/system/src/Grav/Common/Page/ParsedownExtraUtil.php @@ -0,0 +1,21 @@ +' inside code fences +function escapeFences($text) +{ + $rx = "#(^|\n)```.*?\n```#s"; + return preg_replace_callback($rx, function ($m) { + $match = $m[0]; + $ret = str_replace("<", "___LT___", $match); + $ret = str_replace(">", "___GT___", $ret); + return $ret; + + }, $text); +} + +function unescapeFences($text) +{ + $text = str_replace("___LT___", "<", $text); + $text = str_replace("___GT___", ">", $text); + return $text; +} \ No newline at end of file diff --git a/user/config/system.yaml b/user/config/system.yaml index 474d8edfdb..c9a792167a 100644 --- a/user/config/system.yaml +++ b/user/config/system.yaml @@ -7,6 +7,7 @@ pages: theme: antimatter markdown: extra: false + extra_escape_fences: true process: markdown: true twig: false From 524d71eb45123d55be301dfaef248c3c60526301 Mon Sep 17 00:00:00 2001 From: Steven Spungin Date: Tue, 6 Feb 2018 11:15:52 -0500 Subject: [PATCH 2/3] Add i8n defines. Move ParsedownExtraUtil to Markdown folder. --- system/blueprints/config/system.yaml | 4 ++-- .../src/Grav/Common/{Page => Markdown}/ParsedownExtraUtil.php | 0 system/src/Grav/Common/Page/Page.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename system/src/Grav/Common/{Page => Markdown}/ParsedownExtraUtil.php (100%) diff --git a/system/blueprints/config/system.yaml b/system/blueprints/config/system.yaml index 7efb8a7051..d563f34fd9 100644 --- a/system/blueprints/config/system.yaml +++ b/system/blueprints/config/system.yaml @@ -422,7 +422,7 @@ form: fields: pages.markdown.extra: type: toggle - label: Markdown extra + label: PLUGIN_ADMIN.MARKDOWN_EXTRA help: PLUGIN_ADMIN.MARKDOWN_EXTRA_HELP highlight: 0 options: @@ -432,7 +432,7 @@ form: type: bool pages.markdown.extra_escape_fences: type: toggle - label: Escape HTML elements in markdown extra fences + label: PLUGIN_ADMIN.MARKDOWN_EXTRA_ESCAPE_FENCES help: PLUGIN_ADMIN.MARKDOWN_EXTRA_ESCAPE_FENCES_HELP highlight: 1 options: diff --git a/system/src/Grav/Common/Page/ParsedownExtraUtil.php b/system/src/Grav/Common/Markdown/ParsedownExtraUtil.php similarity index 100% rename from system/src/Grav/Common/Page/ParsedownExtraUtil.php rename to system/src/Grav/Common/Markdown/ParsedownExtraUtil.php diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 328ea19ad3..42dd6c8405 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -772,7 +772,7 @@ protected function processMarkdown() if ($defaults['extra']) { $parsedown = new ParsedownExtra($this, $defaults); if ($config->get("system.pages.markdown.extra_escape_fences")) { - include_once "ParsedownExtraUtil.php"; + include_once __DIR__ . "/../Markdown/ParsedownExtraUtil.php"; $content = escapeFences($content); $needToUnescape = true; } From d27d2c16253fcd9f33686fd934a703b1ec5da196 Mon Sep 17 00:00:00 2001 From: Steven Spungin Date: Fri, 23 Feb 2018 07:46:22 -0500 Subject: [PATCH 3/3] Add i8n defines. Move ParsedownExtraUtil to Markdown folder. --- system/src/Grav/Common/Page/Page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 42dd6c8405..403ed857fa 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -17,6 +17,7 @@ use Grav\Common\Language\Language; use Grav\Common\Markdown\Parsedown; use Grav\Common\Markdown\ParsedownExtra; +use Grav\Common\Markdown\ParsedownExtraUtil; use Grav\Common\Taxonomy; use Grav\Common\Uri; use Grav\Common\Utils; @@ -772,7 +773,6 @@ protected function processMarkdown() if ($defaults['extra']) { $parsedown = new ParsedownExtra($this, $defaults); if ($config->get("system.pages.markdown.extra_escape_fences")) { - include_once __DIR__ . "/../Markdown/ParsedownExtraUtil.php"; $content = escapeFences($content); $needToUnescape = true; }