mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-04 17:21:39 +09:00
Update Markdown formatter to support footnotes and hard wraps natively
This commit is contained in:
parent
cbd324c35b
commit
464325c6f6
9 changed files with 106 additions and 19 deletions
|
|
@ -98,22 +98,25 @@ class Formatter
|
|||
*/
|
||||
public static function markdown2html($markdown, $options = 0)
|
||||
{
|
||||
if ($options & self::MD_NEWLINE_AS_BR)
|
||||
{
|
||||
$markdown = preg_replace('/(?<!\n)\n(?![\n\*\#\-])/', " \n", $markdown);
|
||||
}
|
||||
|
||||
if ($options & self::MD_ENABLE_EXTRA)
|
||||
{
|
||||
$class_name = '\\Michelf\\MarkdownExtra';
|
||||
$classes = array('footnote-ref', 'footnote-backref');
|
||||
$parser = new \Michelf\MarkdownExtra;
|
||||
$parser->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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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 .
|
||||
This is an .
|
||||
|
||||
- This is a list.
|
||||
- It has two items.
|
||||
|
|
|
|||
|
|
@ -2,16 +2,16 @@
|
|||
These lines belong in the same paragraph.<br />
|
||||
Markdown usually ignores single line breaks.</p>
|
||||
|
||||
<pre><code>This is an indented code segment.
|
||||
<pre><code>This is an indented code segment.
|
||||
All Markdown variants will recognize it.
|
||||
</code></pre>
|
||||
|
||||
<p><code>This is a fenced code segment.
|
||||
<p><code>This is a fenced code segment.
|
||||
Only Markdown Extra will recognize it.</code></p>
|
||||
|
||||
<p>This is a <a href="foobar.html">link</a>.<br />
|
||||
This is an <img src="foobar.jpg" alt="image" />.</p>
|
||||
|
||||
<ul><li>This is a list. </li>
|
||||
<li>It has two items. </li>
|
||||
<ul><li>This is a list.</li>
|
||||
<li>It has two items.</li>
|
||||
</ul>
|
||||
|
|
@ -2,17 +2,17 @@
|
|||
These lines belong in the same paragraph.<br />
|
||||
Markdown usually ignores single line breaks.</p>
|
||||
|
||||
<pre><code>This is an indented code segment.
|
||||
<pre><code>This is an indented code segment.
|
||||
All Markdown variants will recognize it.
|
||||
</code></pre>
|
||||
|
||||
<pre><code>This is a fenced code segment.
|
||||
Only Markdown Extra will recognize it.
|
||||
<pre><code>This is a fenced code segment.
|
||||
Only Markdown Extra will recognize it.
|
||||
</code></pre>
|
||||
|
||||
<p>This is a <a href="foobar.html">link</a>.<br />
|
||||
This is an <img src="foobar.jpg" alt="image" />.</p>
|
||||
|
||||
<ul><li>This is a list. </li>
|
||||
<li>It has two items. </li>
|
||||
<ul><li>This is a list.</li>
|
||||
<li>It has two items.</li>
|
||||
</ul>
|
||||
18
tests/_data/formatter/markdown2html.target4.html
Normal file
18
tests/_data/formatter/markdown2html.target4.html
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<p>This is a Markdown document.
|
||||
These lines belong in the same paragraph.
|
||||
Markdown usually ignores single line breaks.</p>
|
||||
|
||||
<pre><code>This is an indented code segment.
|
||||
All Markdown variants will recognize it.
|
||||
</code></pre>
|
||||
|
||||
<pre><code>This is a fenced code segment.
|
||||
Only Markdown Extra will recognize it.
|
||||
</code></pre>
|
||||
|
||||
<p>This is a <a href="foobar.html">link</a>.
|
||||
This is an <img src="foobar.jpg" alt="image" />.</p>
|
||||
|
||||
<ul><li>This is a list.</li>
|
||||
<li>It has two items.</li>
|
||||
</ul>
|
||||
16
tests/_data/formatter/markdownextra.source.md
Normal file
16
tests/_data/formatter/markdownextra.source.md
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<div markdown="1">
|
||||
This is *true* markdown text.
|
||||
</div>
|
||||
|
||||
This sentence has a footnote. [^1]
|
||||
|
||||
[^1]: Here's the footnote content.
|
||||
|
||||
```
|
||||
fenced code block
|
||||
```
|
||||
|
||||
Hello | World
|
||||
----- | ------
|
||||
Foo | Bar
|
||||
Baz | Rhymix
|
||||
14
tests/_data/formatter/markdownextra.target1.html
Normal file
14
tests/_data/formatter/markdownextra.target1.html
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<div>
|
||||
This is *true* markdown text.
|
||||
</div>
|
||||
|
||||
<p>This sentence has a footnote. [^1]</p>
|
||||
|
||||
<p>[^1]: Here's the footnote content.</p>
|
||||
|
||||
<p><code>fenced code block</code></p>
|
||||
|
||||
<p>Hello | World
|
||||
----- | ------
|
||||
Foo | Bar
|
||||
Baz | Rhymix</p>
|
||||
24
tests/_data/formatter/markdownextra.target2.html
Normal file
24
tests/_data/formatter/markdownextra.target2.html
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<div>
|
||||
|
||||
<pre><code>This is *true* markdown text.
|
||||
</code></pre>
|
||||
|
||||
</div>
|
||||
|
||||
<p>This sentence has a footnote. <sup id="user_content_fnref:user_content_1"><a href="#fn:user_content_1" class="footnote-ref">1</a></sup></p>
|
||||
|
||||
<pre><code>fenced code block
|
||||
</code></pre>
|
||||
|
||||
<table><thead><tr><th>Hello</th>
|
||||
<th>World</th>
|
||||
</tr></thead><tbody><tr><td>Foo</td>
|
||||
<td>Bar</td>
|
||||
</tr><tr><td>Baz</td>
|
||||
<td>Rhymix</td>
|
||||
</tr></tbody></table><div>
|
||||
<hr /><ol><li id="user_content_fn:user_content_1">
|
||||
<p>Here's the footnote content. <a href="#fnref:user_content_1" class="footnote-backref">↩︎</a></p>
|
||||
</li>
|
||||
|
||||
</ol></div>
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue