Skip to content

Commit

Permalink
better handling of continued inline HTML in paragraphs
Browse files Browse the repository at this point in the history
fixes cebe#114
  • Loading branch information
cebe committed Feb 9, 2016
1 parent 35d47fe commit e449935
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
11 changes: 10 additions & 1 deletion GithubMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ protected function consumeParagraph($lines, $current)
|| ltrim($line) === ''
|| !ctype_alpha($line[0]) && (
$this->identifyQuote($line, $lines, $i) ||
$this->identifyCode($line, $lines, $i) ||
$this->identifyFencedCode($line, $lines, $i) ||
$this->identifyUl($line, $lines, $i) ||
$this->identifyOl($line, $lines, $i) ||
Expand All @@ -77,6 +76,16 @@ protected function consumeParagraph($lines, $current)
|| $this->identifyHeadline($line, $lines, $i))
{
break;
} elseif ($this->identifyCode($line, $lines, $i)) {
// possible beginning of a code block
// but check for continued inline HTML
// e.g. <img src="file.jpg"
// alt="some alt aligned with src attribute" title="some text" />
if (preg_match('~<\w+([^>]+)$~s', implode("\n", $content))) {
$content[] = $line;
} else {
break;
}
} else {
$content[] = $line;
}
Expand Down
19 changes: 13 additions & 6 deletions Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,20 @@ protected function consumeParagraph($lines, $current)
break;
}

if ($line !== '' && ltrim($line) !== '' &&
!($line[0] === "\t" || $line[0] === " " && strncmp($line, ' ', 4) === 0) &&
!$this->identifyHeadline($line, $lines, $i))
{
$content[] = $line;
} else {
if ($line === '' || ltrim($line) === '' || $this->identifyHeadline($line, $lines, $i)) {
break;
} elseif ($line[0] === "\t" || $line[0] === " " && strncmp($line, ' ', 4) === 0) {
// possible beginning of a code block
// but check for continued inline HTML
// e.g. <img src="file.jpg"
// alt="some alt aligned with src attribute" title="some text" />
if (preg_match('~<\w+([^>]+)$~s', implode("\n", $content))) {
$content[] = $line;
} else {
break;
}
} else {
$content[] = $line;
}
}
$block = [
Expand Down
4 changes: 2 additions & 2 deletions block/HtmlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ protected function renderInlineHtml($block)
protected function parseInlineHtml($text)
{
if (strpos($text, '>') !== false) {
if (preg_match('~^</?(\w+\d?)( .*?)?>~', $text, $matches)) {
if (preg_match('~^</?(\w+\d?)( .*?)?>~s', $text, $matches)) {
// HTML tags
return [['inlineHtml', $matches[0]], strlen($matches[0])];
} elseif (preg_match('~^<!--.*?-->~', $text, $matches)) {
} elseif (preg_match('~^<!--.*?-->~s', $text, $matches)) {
// HTML comments
return [['inlineHtml', $matches[0]], strlen($matches[0])];
}
Expand Down
18 changes: 18 additions & 0 deletions tests/markdown-data/html-block.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<p>more markdown here</p>
<p>&lt; this is not an html tag</p>
<p>&lt;thisisnotanhtmltag</p>
<p>but this is:</p>
<p><img src="file.jpg"
alt="some alt aligned with src attribute" title="some text" /></p>
<p><span class="test">some inline <strong>md</strong></span></p>
<p><span>some inline <strong>md</strong></span></p>
<p>self-closing on block level:</p>
Expand All @@ -22,3 +25,18 @@
<h1>h1</h1>
<custom multi="line" something="hi" />
<h2>h2</h2>
<p>p <img src="file.jpg"
alt="some alt aligned with src attribute"
title="some text" />
something</p>
<p>p <img src="file.jpg"
alt="some alt aligned with src attribute"
title="some text" /></p>
<pre><code>something
</code></pre>
<p>p is &lt; than 5</p>
<pre><code>this is code
</code></pre>
<p>this paragraph contains a <!-- multi
line html comment -->
newline</p>
22 changes: 22 additions & 0 deletions tests/markdown-data/html-block.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ more markdown here

<thisisnotanhtmltag

but this is:

<img src="file.jpg"
alt="some alt aligned with src attribute" title="some text" />

<span class="test">some inline **md**</span>

<span>some inline **md**</span>
Expand All @@ -35,3 +40,20 @@ something **bold**.
<custom multi="line" something="hi" />

## h2

p <img src="file.jpg"
alt="some alt aligned with src attribute"
title="some text" />
something

p <img src="file.jpg"
alt="some alt aligned with src attribute"
title="some text" />
something

p is < than 5
this is code

this paragraph contains a <!-- multi
line html comment -->
newline

0 comments on commit e449935

Please sign in to comment.