Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/hotfix markdown fences #1885

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion system/blueprints/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,24 @@ form:
fields:
pages.markdown.extra:
type: toggle
label: Markdown extra
label: PLUGIN_ADMIN.MARKDOWN_EXTRA
help: PLUGIN_ADMIN.MARKDOWN_EXTRA_HELP
highlight: 0
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool
pages.markdown.extra_escape_fences:
type: toggle
label: PLUGIN_ADMIN.MARKDOWN_EXTRA_ESCAPE_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
Expand Down
21 changes: 21 additions & 0 deletions system/src/Grav/Common/Markdown/ParsedownExtraUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

// Escape '<' and '>' 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___", "&lt;", $text);
$text = str_replace("___GT___", "&gt;", $text);
return $text;
}
18 changes: 16 additions & 2 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -766,14 +767,27 @@ 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")) {
$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;
}


Expand Down
1 change: 1 addition & 0 deletions user/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pages:
theme: antimatter
markdown:
extra: false
extra_escape_fences: true
process:
markdown: true
twig: false
Expand Down