hhvm / fbmarkdown

Pure-Hack implementation of GitHub Flavored Markdown, based on the specification at https://github.github.com/gfm/
MIT License
29 stars 11 forks source link

CRLF is not supported #5

Closed Kilenaitor closed 6 years ago

Kilenaitor commented 6 years ago

Rendering of blockquotes is currently broken. No matter how many newlines exist after the blockquote line ends it still rendered as part of that blockquote.

Example 1 Raw:

screen shot 2018-04-30 at 1 17 10 am

Rendered:

screen shot 2018-04-30 at 1 17 20 am

Example 2

Raw:

screen shot 2018-04-30 at 1 18 00 am

Rendered:

screen shot 2018-04-30 at 1 18 09 am

Relevant render function

private function markdownToHTML(string $markdown): string {
    $ctx = (new Markdown\ParserContext())->enableHTML_UNSAFE();
    $ast = Markdown\parse($ctx, $markdown);

    $html =
      (new Markdown\HTMLRenderer(new Markdown\RenderContext()))->render($ast);

    return $html;
  }

Raw input:

Blockquotes are broken. 

> Hello I am a blockquote. 

Hello I am not a blockquote. I should come after.

HTML output from render:

<p>Blockquotes are broken.</p>
<blockquote>
<p>Hello I am a blockquote.

Hello I am not a blockquote. I should come after.</p>
</blockquote>
fredemmott commented 6 years ago

Thanks - what version are you using? I'm not able to reproduce the problem with this script, or by attempting to add to unit tests:

https://gist.github.com/fredemmott/95e9e497622891077fd7855a67061a30

Kilenaitor commented 6 years ago

Hey Fred. I'm using v1.0; latest version I see on packagist. Actually realized the issue. JSON-encoded the string and printed it out. The one I was getting from the database had Windows-style newlines \r\n instead of just the standard \n.

I did a simple

$markdown = preg_replace('~\r\n?~', "\n", $markdown);

where $markdown is my string and it was properly parsed by fbmarkdown. So it looks like it's a thing regarding newlines. Not sure if it's something to fix or at least better document, but yeah; doing the preg_replace beforehand fixed it. :)

fredemmott commented 6 years ago

Thanks - yep, this is a bug :)

From spec:

A line ending is a newline (U+000A), a carriage return (U+000D) not followed by a newline, or a carriage return and a following newline.

fbmarkdown incorrectly only accepts newlines as line endings.