From 38ea813b0ea950cf34d5bfde04c23191c70a2c13 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Thu, 5 Apr 2018 16:54:35 +0100 Subject: [PATCH 1/2] Add failing test case --- test/data/fenced_code_block.html | 5 ++++- test/data/fenced_code_block.md | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/test/data/fenced_code_block.html b/test/data/fenced_code_block.html index 565a541..78480fd 100644 --- a/test/data/fenced_code_block.html +++ b/test/data/fenced_code_block.html @@ -8,4 +8,7 @@ echo $message;
<?php
 echo "Hello World";
 ?>
-<a href="http://auraphp.com" >Aura Project</a>
\ No newline at end of file +<a href="http://auraphp.com" >Aura Project</a> +
the following isn't quite enough to close
+```
+still a fenced code block
\ No newline at end of file diff --git a/test/data/fenced_code_block.md b/test/data/fenced_code_block.md index 62db24a..35acb75 100644 --- a/test/data/fenced_code_block.md +++ b/test/data/fenced_code_block.md @@ -22,4 +22,10 @@ echo 'language identifier with non words'; echo "Hello World"; ?> Aura Project -``` \ No newline at end of file +``` + +```` +the following isn't quite enough to close +``` +still a fenced code block +```` \ No newline at end of file From 06b810cd4a4e139d7367c70dc1470d6a7f1c06bb Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Thu, 5 Apr 2018 16:55:14 +0100 Subject: [PATCH 2/2] Fix fenced code block closer to match CommonMark rules --- Parsedown.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 186896c..ee38601 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -443,16 +443,16 @@ class Parsedown protected function blockFencedCode($Line) { - if (preg_match('/^['.$Line['text'][0].']{3,}[ ]*([^`]+)?[ ]*$/', $Line['text'], $matches)) + if (preg_match('/^(['.$Line['text'][0].']{3,})[ ]*([^`]+)?[ ]*$/', $Line['text'], $matches)) { $Element = array( 'name' => 'code', 'text' => '', ); - if (isset($matches[1])) + if (isset($matches[2])) { - $class = 'language-'.$matches[1]; + $class = 'language-'.$matches[2]; $Element['attributes'] = array( 'class' => $class, @@ -461,6 +461,7 @@ class Parsedown $Block = array( 'char' => $Line['text'][0], + 'openerLength' => mb_strlen($matches[1]), 'element' => array( 'name' => 'pre', 'element' => $Element, @@ -485,8 +486,10 @@ class Parsedown unset($Block['interrupted']); } - if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text'])) - { + if ( + preg_match('/^(['.preg_quote($Block['char']).']{3,})[ ]*$/', $Line['text'], $matches) + and mb_strlen($matches[1]) >= $Block['openerLength'] + ) { $Block['element']['element']['text'] = substr($Block['element']['element']['text'], 1); $Block['complete'] = true;