From 8a9058621824ff0a675a735fbbf1c1161d596208 Mon Sep 17 00:00:00 2001 From: Nathan Baulch Date: Thu, 29 Mar 2018 22:10:30 +1100 Subject: [PATCH 1/4] Support #hashtag per CommonMark and GFM specs --- Parsedown.php | 5 +++++ test/data/atx_heading.html | 4 +++- test/data/atx_heading.md | 6 +++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 382023c..1578815 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -519,6 +519,11 @@ class Parsedown } $text = trim($Line['text'], '#'); + + if (!isset($text[0]) or $text[0] !== ' ') { + return; + } + $text = trim($text, ' '); $Block = array( diff --git a/test/data/atx_heading.html b/test/data/atx_heading.html index 94c3825..545d889 100644 --- a/test/data/atx_heading.html +++ b/test/data/atx_heading.html @@ -7,5 +7,7 @@

####### not a heading

closed h1

#

+

##

# of levels

-

# of levels #

\ No newline at end of file +

# of levels #

+

#hashtag

\ No newline at end of file diff --git a/test/data/atx_heading.md b/test/data/atx_heading.md index 62fdd6c..7748aa9 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 # # + +#hashtag \ No newline at end of file From d0279cdd3b54caa8f7620b10c4397c20ba8cab9e Mon Sep 17 00:00:00 2001 From: Nathan Baulch Date: Fri, 30 Mar 2018 03:44:47 +1100 Subject: [PATCH 2/4] Enable #hashtag support via setting --- Parsedown.php | 11 ++++++++++- test/ParsedownTest.php | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) mode change 100644 => 100755 Parsedown.php mode change 100644 => 100755 test/ParsedownTest.php diff --git a/Parsedown.php b/Parsedown.php old mode 100644 new mode 100755 index 1578815..ff1ae0e --- a/Parsedown.php +++ b/Parsedown.php @@ -90,6 +90,15 @@ class Parsedown protected $safeMode; + function setHastagsEnabled($hashtagsEnabled) + { + $this->hashtagsEnabled = (bool) $hashtagsEnabled; + + return $this; + } + + protected $hashtagsEnabled; + protected $safeLinksWhitelist = array( 'http://', 'https://', @@ -520,7 +529,7 @@ class Parsedown $text = trim($Line['text'], '#'); - if (!isset($text[0]) or $text[0] !== ' ') { + if ($this->hashtagsEnabled and (!isset($text[0]) or $text[0] !== ' ')) { return; } diff --git a/test/ParsedownTest.php b/test/ParsedownTest.php old mode 100644 new mode 100755 index 01b4e25..f4c5f7c --- a/test/ParsedownTest.php +++ b/test/ParsedownTest.php @@ -32,6 +32,7 @@ class ParsedownTest extends TestCase protected function initParsedown() { $Parsedown = new TestParsedown(); + $Parsedown->setHastagsEnabled(true); return $Parsedown; } From cf6d23de550baa24ed9f44ff4e58c888f885248e Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Mon, 2 Apr 2018 17:06:31 +0100 Subject: [PATCH 3/4] Rename hashtags enabled to strict mode We can use this to seperate any intentional spec deviations from spec behaviour so users can pick between compatability and spec implementations --- Parsedown.php | 9 +++++---- test/ParsedownTest.php | 2 +- test/data/atx_heading.html | 3 +-- test/data/atx_heading.md | 4 +--- test/data/strict_atx_heading.html | 13 +++++++++++++ test/data/strict_atx_heading.md | 25 +++++++++++++++++++++++++ 6 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 test/data/strict_atx_heading.html create mode 100644 test/data/strict_atx_heading.md diff --git a/Parsedown.php b/Parsedown.php index ff1ae0e..a3efd7a 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -90,14 +90,14 @@ class Parsedown protected $safeMode; - function setHastagsEnabled($hashtagsEnabled) + function setStrictMode($strictMode) { - $this->hashtagsEnabled = (bool) $hashtagsEnabled; + $this->strictMode = (bool) $strictMode; return $this; } - protected $hashtagsEnabled; + protected $strictMode; protected $safeLinksWhitelist = array( 'http://', @@ -529,7 +529,8 @@ class Parsedown $text = trim($Line['text'], '#'); - if ($this->hashtagsEnabled and (!isset($text[0]) or $text[0] !== ' ')) { + if ($this->strictMode and ( ! isset($text[0]) or $text[0] !== ' ')) + { return; } diff --git a/test/ParsedownTest.php b/test/ParsedownTest.php index f4c5f7c..bf40317 100755 --- a/test/ParsedownTest.php +++ b/test/ParsedownTest.php @@ -32,7 +32,6 @@ class ParsedownTest extends TestCase protected function initParsedown() { $Parsedown = new TestParsedown(); - $Parsedown->setHastagsEnabled(true); return $Parsedown; } @@ -52,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 545d889..387e2ec 100644 --- a/test/data/atx_heading.html +++ b/test/data/atx_heading.html @@ -7,7 +7,6 @@

####### not a heading

closed h1

#

-

##

# of levels

# of levels #

-

#hashtag

\ No newline at end of file +

heading

\ No newline at end of file diff --git a/test/data/atx_heading.md b/test/data/atx_heading.md index 7748aa9..3724c1f 100644 --- a/test/data/atx_heading.md +++ b/test/data/atx_heading.md @@ -16,10 +16,8 @@ # -## - # # of levels # # of levels # # -#hashtag \ No newline at end of file +#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 From 772c919b058f68d2fdd551a22128d3a65d8cc6d2 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Mon, 2 Apr 2018 17:09:54 +0100 Subject: [PATCH 4/4] Fix bug where empty atx headings would not be recognised (CommonMark) Fixes #595 --- Parsedown.php | 65 ++++++++++++++++++-------------------- test/data/atx_heading.html | 3 +- test/data/atx_heading.md | 2 ++ 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index a3efd7a..1dde17d 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -513,42 +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'], '#'); - - if ($this->strictMode and ( ! isset($text[0]) or $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; + $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/data/atx_heading.html b/test/data/atx_heading.html index 387e2ec..3b09c38 100644 --- a/test/data/atx_heading.html +++ b/test/data/atx_heading.html @@ -6,7 +6,8 @@
h6

####### not a heading

closed h1

-

#

+

+

# of levels

# of levels #

heading

\ No newline at end of file diff --git a/test/data/atx_heading.md b/test/data/atx_heading.md index 3724c1f..50f991c 100644 --- a/test/data/atx_heading.md +++ b/test/data/atx_heading.md @@ -16,6 +16,8 @@ # +## + # # of levels # # of levels # #