diff --git a/common/framework/formatter.php b/common/framework/formatter.php index 4098f4bb1..dc3cf3313 100644 --- a/common/framework/formatter.php +++ b/common/framework/formatter.php @@ -98,22 +98,25 @@ class Formatter */ public static function markdown2html($markdown, $options = 0) { - if ($options & self::MD_NEWLINE_AS_BR) - { - $markdown = preg_replace('/(?fn_id_prefix = 'user_content_'; } else { - $class_name = '\\Michelf\\Markdown'; + $classes = false; + $parser = new \Michelf\Markdown; } - $html = $class_name::defaultTransform($markdown); - return Filters\HTMLFilter::clean($html); + if ($options & self::MD_NEWLINE_AS_BR) + { + $parser->hard_wrap = true; + } + + $html = $parser->transform($markdown); + return Filters\HTMLFilter::clean($html, $classes); } /** diff --git a/tests/_data/formatter/html2markdown.target.md b/tests/_data/formatter/html2markdown.target.md index 77f8ba106..8f3b07c3a 100644 --- a/tests/_data/formatter/html2markdown.target.md +++ b/tests/_data/formatter/html2markdown.target.md @@ -1,7 +1,7 @@ This is a Markdown document. These lines belong in the same paragraph. Markdown usually ignores single line breaks. This is a [link](foobar.html "title"). - This is an ![alt](foobar.jpg "image"). +This is an ![alt](foobar.jpg "image"). - This is a list. - It has two items. diff --git a/tests/_data/formatter/markdown2html.target2.html b/tests/_data/formatter/markdown2html.target2.html index eadda1be2..81f890633 100644 --- a/tests/_data/formatter/markdown2html.target2.html +++ b/tests/_data/formatter/markdown2html.target2.html @@ -2,16 +2,16 @@ These lines belong in the same paragraph.
Markdown usually ignores single line breaks.

-
This is an indented code segment.  
+
This is an indented code segment.
 All Markdown variants will recognize it.
 
-

This is a fenced code segment. +

This is a fenced code segment. Only Markdown Extra will recognize it.

This is a link.
This is an image.

-
  • This is a list.
  • -
  • It has two items.
  • +
    • This is a list.
    • +
    • It has two items.
    \ No newline at end of file diff --git a/tests/_data/formatter/markdown2html.target3.html b/tests/_data/formatter/markdown2html.target3.html index 70d90810d..9432af43f 100644 --- a/tests/_data/formatter/markdown2html.target3.html +++ b/tests/_data/formatter/markdown2html.target3.html @@ -2,17 +2,17 @@ These lines belong in the same paragraph.
    Markdown usually ignores single line breaks.

    -
    This is an indented code segment.  
    +
    This is an indented code segment.
     All Markdown variants will recognize it.
     
    -
    This is a fenced code segment.  
    -Only Markdown Extra will recognize it.  
    +
    This is a fenced code segment.
    +Only Markdown Extra will recognize it.
     

    This is a link.
    This is an image.

    -
    • This is a list.
    • -
    • It has two items.
    • +
      • This is a list.
      • +
      • It has two items.
      \ No newline at end of file diff --git a/tests/_data/formatter/markdown2html.target4.html b/tests/_data/formatter/markdown2html.target4.html new file mode 100644 index 000000000..7d430da66 --- /dev/null +++ b/tests/_data/formatter/markdown2html.target4.html @@ -0,0 +1,18 @@ +

      This is a Markdown document. +These lines belong in the same paragraph. +Markdown usually ignores single line breaks.

      + +
      This is an indented code segment.
      +All Markdown variants will recognize it.
      +
      + +
      This is a fenced code segment.
      +Only Markdown Extra will recognize it.
      +
      + +

      This is a link. +This is an image.

      + +
      • This is a list.
      • +
      • It has two items.
      • +
      \ No newline at end of file diff --git a/tests/_data/formatter/markdownextra.source.md b/tests/_data/formatter/markdownextra.source.md new file mode 100644 index 000000000..4587c270d --- /dev/null +++ b/tests/_data/formatter/markdownextra.source.md @@ -0,0 +1,16 @@ +
      + This is *true* markdown text. +
      + +This sentence has a footnote. [^1] + +[^1]: Here's the footnote content. + +``` +fenced code block +``` + +Hello | World +----- | ------ +Foo | Bar +Baz | Rhymix diff --git a/tests/_data/formatter/markdownextra.target1.html b/tests/_data/formatter/markdownextra.target1.html new file mode 100644 index 000000000..6563610b3 --- /dev/null +++ b/tests/_data/formatter/markdownextra.target1.html @@ -0,0 +1,14 @@ +
      + This is *true* markdown text. +
      + +

      This sentence has a footnote. [^1]

      + +

      [^1]: Here's the footnote content.

      + +

      fenced code block

      + +

      Hello | World +----- | ------ +Foo | Bar +Baz | Rhymix

      diff --git a/tests/_data/formatter/markdownextra.target2.html b/tests/_data/formatter/markdownextra.target2.html new file mode 100644 index 000000000..916441aaa --- /dev/null +++ b/tests/_data/formatter/markdownextra.target2.html @@ -0,0 +1,24 @@ +
      + +
      This is *true* markdown text.
      +
      + +
      + +

      This sentence has a footnote. 1

      + +
      fenced code block
      +
      + + + + + + + +
      HelloWorld
      FooBar
      BazRhymix
      +
      1. +

        Here's the footnote content. ↩︎

        +
      2. + +
      diff --git a/tests/unit/framework/FormatterTest.php b/tests/unit/framework/FormatterTest.php index 1f8e26c37..7688cd7df 100644 --- a/tests/unit/framework/FormatterTest.php +++ b/tests/unit/framework/FormatterTest.php @@ -28,10 +28,22 @@ class FormatterTest extends \Codeception\TestCase\Test $html1 = file_get_contents(\RX_BASEDIR . 'tests/_data/formatter/markdown2html.target1.html'); $html2 = file_get_contents(\RX_BASEDIR . 'tests/_data/formatter/markdown2html.target2.html'); $html3 = file_get_contents(\RX_BASEDIR . 'tests/_data/formatter/markdown2html.target3.html'); + $html4 = file_get_contents(\RX_BASEDIR . 'tests/_data/formatter/markdown2html.target4.html'); $this->assertEquals($html1, Rhymix\Framework\Formatter::markdown2html($markdown)); $this->assertEquals($html2, Rhymix\Framework\Formatter::markdown2html($markdown, Rhymix\Framework\Formatter::MD_NEWLINE_AS_BR)); $this->assertEquals($html3, Rhymix\Framework\Formatter::markdown2html($markdown, Rhymix\Framework\Formatter::MD_NEWLINE_AS_BR | Rhymix\Framework\Formatter::MD_ENABLE_EXTRA)); + $this->assertEquals($html4, Rhymix\Framework\Formatter::markdown2html($markdown, Rhymix\Framework\Formatter::MD_ENABLE_EXTRA)); + } + + public function testMarkdownExtra() + { + $markdown = file_get_contents(\RX_BASEDIR . 'tests/_data/formatter/markdownextra.source.md'); + $html1 = file_get_contents(\RX_BASEDIR . 'tests/_data/formatter/markdownextra.target1.html'); + $html2 = file_get_contents(\RX_BASEDIR . 'tests/_data/formatter/markdownextra.target2.html'); + + $this->assertEquals($html1, Rhymix\Framework\Formatter::markdown2html($markdown)); + $this->assertEquals($html2, Rhymix\Framework\Formatter::markdown2html($markdown, Rhymix\Framework\Formatter::MD_ENABLE_EXTRA)); } public function testHTML2Markdown()