diff --git a/Parsedown.php b/Parsedown.php old mode 100644 new mode 100755 index 382023c..1dde17d --- a/Parsedown.php +++ b/Parsedown.php @@ -90,6 +90,15 @@ class Parsedown protected $safeMode; + function setStrictMode($strictMode) + { + $this->strictMode = (bool) $strictMode; + + return $this; + } + + protected $strictMode; + protected $safeLinksWhitelist = array( 'http://', 'https://', @@ -504,36 +513,39 @@ class Parsedown protected function blockHeader($Line) { - if (isset($Line['text'][1])) + $level = 1; + + while (isset($Line['text'][$level]) and $Line['text'][$level] === '#') { - $level = 1; - - while (isset($Line['text'][$level]) and $Line['text'][$level] === '#') - { - $level ++; - } - - if ($level > 6) - { - return; - } - - $text = trim($Line['text'], '#'); - $text = trim($text, ' '); - - $Block = array( - 'element' => array( - 'name' => 'h' . min(6, $level), - 'handler' => array( - 'function' => 'lineElements', - 'argument' => $text, - 'destination' => 'elements', - ) - ), - ); - - return $Block; + $level ++; } + + if ($level > 6) + { + return; + } + + $text = trim($Line['text'], '#'); + + if ($this->strictMode and isset($text[0]) and $text[0] !== ' ') + { + return; + } + + $text = trim($text, ' '); + + $Block = array( + 'element' => array( + 'name' => 'h' . min(6, $level), + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $text, + 'destination' => 'elements', + ) + ), + ); + + return $Block; } # diff --git a/test/ParsedownTest.php b/test/ParsedownTest.php old mode 100644 new mode 100755 index 01b4e25..bf40317 --- a/test/ParsedownTest.php +++ b/test/ParsedownTest.php @@ -51,6 +51,7 @@ class ParsedownTest extends TestCase $expectedMarkup = str_replace("\r", "\n", $expectedMarkup); $this->Parsedown->setSafeMode(substr($test, 0, 3) === 'xss'); + $this->Parsedown->setStrictMode(substr($test, 0, 6) === 'strict'); $actualMarkup = $this->Parsedown->text($markdown); diff --git a/test/data/atx_heading.html b/test/data/atx_heading.html index 94c3825..3b09c38 100644 --- a/test/data/atx_heading.html +++ b/test/data/atx_heading.html @@ -6,6 +6,8 @@
h6

####### not a heading

closed h1

-

#

+

+

# of levels

-

# of levels #

\ No newline at end of file +

# of levels #

+

heading

\ No newline at end of file diff --git a/test/data/atx_heading.md b/test/data/atx_heading.md index 62fdd6c..50f991c 100644 --- a/test/data/atx_heading.md +++ b/test/data/atx_heading.md @@ -16,6 +16,10 @@ # +## + # # of levels -# # of levels # # \ No newline at end of file +# # of levels # # + +#heading \ No newline at end of file diff --git a/test/data/strict_atx_heading.html b/test/data/strict_atx_heading.html new file mode 100644 index 0000000..11cf4df --- /dev/null +++ b/test/data/strict_atx_heading.html @@ -0,0 +1,13 @@ +

h1

+

h2

+

h3

+

h4

+
h5
+
h6
+

####### not a heading

+

#not a heading

+

closed h1

+

+

+

# of levels

+

# of levels #

\ No newline at end of file diff --git a/test/data/strict_atx_heading.md b/test/data/strict_atx_heading.md new file mode 100644 index 0000000..5358731 --- /dev/null +++ b/test/data/strict_atx_heading.md @@ -0,0 +1,25 @@ +# h1 + +## h2 + +### h3 + +#### h4 + +##### h5 + +###### h6 + +####### not a heading + +#not a heading + +# closed h1 # + +# + +## + +# # of levels + +# # of levels # # \ No newline at end of file