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

Use temporary rewriting of curly brackets when rendering Markdown content blocks #994

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
44 changes: 40 additions & 4 deletions modules/cms/classes/Content.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php namespace Cms\Classes;
<?php

use File;
use Markdown;
namespace Cms\Classes;

use Winter\Storm\Support\Facades\File;
use Winter\Storm\Support\Facades\Markdown;

/**
* The CMS content file class.
Expand Down Expand Up @@ -62,12 +64,46 @@ public function parseMarkup()
$result = htmlspecialchars($this->markup);
break;
case 'md':
$result = Markdown::parse($this->markup);
$result = $this->parseMarkdownMarkup($this->markup);
break;
default:
$result = $this->markup;
}

return $result;
}

/**
* Parse Markdown content files.
*
* This method replaces curly variables in the content temporarily while Markdown rendering takes place, to
* circumvent the escaping that Commonmark does on curly brackets in links.
*
* @param string $markup
* @return string
bennothommo marked this conversation as resolved.
Show resolved Hide resolved
*/
protected function parseMarkdownMarkup(string $markup): string
{
/**
* Replace variables temporarily for Markdown parsing, as the Commonmark library escapes curly brackets in
* links.
* @see https://github.com/wintercms/winter/issues/992
*/
$variables = [];
$markup = preg_replace_callback('/\{([^\n \}]+)\}/', function (array $matches) use (&$variables) {
$signature = hash('sha256', $matches[1]);
if (!array_key_exists($signature, $variables)) {
$variables[$signature] = $matches[1];
}
return ".=VAR={$signature}=.";
}, $markup);

$markup = Markdown::parse($markup);

foreach ($variables as $signature => $variable) {
$markup = str_replace(".=VAR={$signature}=.", "{{$variable}}", $markup);
}

return $markup;
}
}