1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Merge pull request #589 from NathanBaulch/hashtag

Support #hashtag per CommonMark and GFM specs
This commit is contained in:
Aidan Woods 2018-04-02 17:25:28 +01:00 committed by GitHub
commit 498c88c4eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 88 additions and 31 deletions

68
Parsedown.php Normal file → Executable file
View File

@ -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;
}
#

1
test/ParsedownTest.php Normal file → Executable file
View File

@ -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);

View File

@ -6,6 +6,8 @@
<h6>h6</h6>
<p>####### not a heading</p>
<h1>closed h1</h1>
<p>#</p>
<h1></h1>
<h2></h2>
<h1># of levels</h1>
<h1># of levels #</h1>
<h1># of levels #</h1>
<h1>heading</h1>

View File

@ -16,6 +16,10 @@
#
##
# # of levels
# # of levels # #
# # of levels # #
#heading

View File

@ -0,0 +1,13 @@
<h1>h1</h1>
<h2>h2</h2>
<h3>h3</h3>
<h4>h4</h4>
<h5>h5</h5>
<h6>h6</h6>
<p>####### not a heading</p>
<p>#not a heading</p>
<h1>closed h1</h1>
<h1></h1>
<h2></h2>
<h1># of levels</h1>
<h1># of levels #</h1>

View File

@ -0,0 +1,25 @@
# h1
## h2
### h3
#### h4
##### h5
###### h6
####### not a heading
#not a heading
# closed h1 #
#
##
# # of levels
# # of levels # #