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

Create ID's for Header elements so they can be referenced in anchor tags #765

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,15 +553,23 @@ protected function blockHeader($Line)
}

$text = trim($text, ' ');
$link = preg_replace('/[^\p{L}\p{N}\p{M}-]+/u', '', mb_strtolower(mb_ereg_replace(' ','-',$text)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we absolutely need the mb_ereg_replace() here? It's not a regular expression, so we should be fine using str_replace(' ', '-', $text). GFM seems to convert consecutive spaces to consecutive dashes as well.

I'm not a decision maker, but as someone who spends a lot of time processing text and with this library, this looks great to me otherwise. Thank you!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, as if you consider a four byte character, one of those could match a space. Thus it would invalidate that character or change it to something it's not meant to be. I never deal with them but if we really want to make it universal, I would suggest we do.

$attr = array();
if (!empty($link)) {
$attr = array(
'id' => $link,
);
}

$Block = array(
'element' => array(
'name' => 'h' . $level,
'attributes' => $attr,
'handler' => array(
'function' => 'lineElements',
'argument' => $text,
'destination' => 'elements',
)
),
),
);

Expand Down Expand Up @@ -1992,3 +2000,4 @@ static function instance($name = 'default')
'wbr', 'time',
);
}

4 changes: 3 additions & 1 deletion test/ParsedownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ function test_($test, $dir)
$this->Parsedown->setStrictMode(substr($test, 0, 6) === 'strict');

$actualMarkup = $this->Parsedown->text($markdown);
$actualMarkup = str_replace("\r\n", "\n", $actualMarkup);
$actualMarkup = str_replace("\r", "\n", $actualMarkup);

$this->assertEquals($expectedMarkup, $actualMarkup);
$this->assertEquals(trim($expectedMarkup), trim($actualMarkup));
}

function testRawHtml()
Expand Down
26 changes: 13 additions & 13 deletions test/data/atx_heading.html
Original file line number Diff line number Diff line change
@@ -1,13 +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>
<h1>closed h1</h1>
<h1></h1>
<h2></h2>
<h1># of levels</h1>
<h1># of levels #</h1>
<h1>heading</h1>
<h1 id="h1">h1</h1>
<h2 id="h2">h2</h2>
<h3 id="h3">h3</h3>
<h4 id="h4">h4</h4>
<h5 id="h5">h5</h5>
<h6 id="h6">h6</h6>
<p>####### not a heading</p>
<h1 id="closed-h1">closed h1</h1>
<h1></h1>
<h2></h2>
<h1 id="#-of-levels"># of levels</h1>
<h1 id="#-of-levels-#"># of levels #</h1>
<h1 id="heading">heading</h1>
18 changes: 9 additions & 9 deletions test/data/strict_atx_heading.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<h1>h1</h1>
<h2>h2</h2>
<h3>h3</h3>
<h4>h4</h4>
<h5>h5</h5>
<h6>h6</h6>
<h1 id="h1">h1</h1>
<h2 id="h2">h2</h2>
<h3 id="h3">h3</h3>
<h4 id="h4">h4</h4>
<h5 id="h5">h5</h5>
<h6 id="h6">h6</h6>
<p>####### not a heading</p>
<p>#not a heading</p>
<h1>closed h1</h1>
<h1 id="closed-h1">closed h1</h1>
<h1></h1>
<h2></h2>
<h1># of levels</h1>
<h1># of levels #</h1>
<h1 id="#-of-levels"># of levels</h1>
<h1 id="#-of-levels-#"># of levels #</h1>