DefaultAttributesExtensionTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Tests\Functional\Extension\DefaultAttributes;
  12. use League\CommonMark\Environment\Environment;
  13. use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
  14. use League\CommonMark\Extension\CommonMark\Node\Block\BlockQuote;
  15. use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
  16. use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
  17. use League\CommonMark\Extension\DefaultAttributes\DefaultAttributesExtension;
  18. use League\CommonMark\Extension\Table\Table;
  19. use League\CommonMark\Extension\Table\TableExtension;
  20. use League\CommonMark\MarkdownConverter;
  21. use League\CommonMark\Node\Block\Paragraph;
  22. use PHPUnit\Framework\TestCase;
  23. final class DefaultAttributesExtensionTest extends TestCase
  24. {
  25. /**
  26. * @dataProvider provideTestCases
  27. *
  28. * @param array<string, mixed> $config
  29. */
  30. public function testExample(string $markdown, array $config, string $expectedHtml): void
  31. {
  32. $environment = new Environment($config);
  33. $environment->addExtension(new CommonMarkCoreExtension());
  34. $environment->addExtension(new TableExtension());
  35. $environment->addExtension(new DefaultAttributesExtension());
  36. $converter = new MarkdownConverter($environment);
  37. $this->assertSame($expectedHtml, \rtrim($converter->convert($markdown)->getContent()));
  38. }
  39. /**
  40. * @return iterable<mixed>
  41. */
  42. public function provideTestCases(): iterable
  43. {
  44. $markdown = <<<MD
  45. # Hello World
  46. Welcome to my blog!
  47. ## About Me
  48. I'm a nerd who **loves** the [league/commonmark](https://commonmark.thephpleague.com) library.
  49. MD;
  50. yield [
  51. $markdown,
  52. [],
  53. <<<HTML
  54. <h1>Hello World</h1>
  55. <p>Welcome to my blog!</p>
  56. <h2>About Me</h2>
  57. <p>I'm a nerd who <strong>loves</strong> the <a href="https://commonmark.thephpleague.com">league/commonmark</a> library.</p>
  58. HTML,
  59. ];
  60. yield [
  61. $markdown,
  62. [
  63. 'default_attributes' => [],
  64. ],
  65. <<<HTML
  66. <h1>Hello World</h1>
  67. <p>Welcome to my blog!</p>
  68. <h2>About Me</h2>
  69. <p>I'm a nerd who <strong>loves</strong> the <a href="https://commonmark.thephpleague.com">league/commonmark</a> library.</p>
  70. HTML,
  71. ];
  72. yield [
  73. $markdown,
  74. [
  75. 'default_attributes' => [
  76. Paragraph::class => [
  77. 'class' => ['text-center', 'font-comic-sans'],
  78. ],
  79. Link::class => [
  80. 'class' => 'btn btn-link',
  81. 'target' => '_blank',
  82. ],
  83. Heading::class => [
  84. 'class' => static function (Heading $node) {
  85. if ($node->getLevel() === 1) {
  86. return 'page-title';
  87. }
  88. return null;
  89. },
  90. ],
  91. ],
  92. ],
  93. <<<HTML
  94. <h1 class="page-title">Hello World</h1>
  95. <p class="text-center font-comic-sans">Welcome to my blog!</p>
  96. <h2>About Me</h2>
  97. <p class="text-center font-comic-sans">I'm a nerd who <strong>loves</strong> the <a class="btn btn-link" target="_blank" href="https://commonmark.thephpleague.com" rel="noopener noreferrer">league/commonmark</a> library.</p>
  98. HTML,
  99. ];
  100. // One last test to theme some Bootstrap 4 content
  101. yield [
  102. <<<MD
  103. # U.S. National Parks
  104. The United States has some amazing national parks.
  105. As William Shakespeare once said:
  106. > One touch of nature makes the whole world kin.
  107. ## My Favorites
  108. | Park | Location | Established |
  109. | ----------- | ----------------------- | ----------------- |
  110. | Yellowstone | Montana, Wyoming, Idaho | March 1, 1872 |
  111. | Yosemite | California | March 1, 1872 |
  112. | Zion | Utah | November 19, 1919 |
  113. MD
  114. ,
  115. [
  116. 'default_attributes' => [
  117. Table::class => [
  118. 'class' => ['table', 'table-responsive'],
  119. ],
  120. BlockQuote::class => [
  121. 'class' => 'blockquote',
  122. ],
  123. Paragraph::class => [
  124. 'class' => static function (Paragraph $paragraph) {
  125. if ($paragraph->previous() instanceof Heading && $paragraph->previous()->getLevel() === 1) {
  126. return 'lead';
  127. }
  128. return null;
  129. },
  130. ],
  131. ],
  132. ],
  133. <<<HTML
  134. <h1>U.S. National Parks</h1>
  135. <p class="lead">The United States has some amazing national parks.</p>
  136. <p>As William Shakespeare once said:</p>
  137. <blockquote class="blockquote">
  138. <p>One touch of nature makes the whole world kin.</p>
  139. </blockquote>
  140. <h2>My Favorites</h2>
  141. <table class="table table-responsive">
  142. <thead>
  143. <tr>
  144. <th>Park</th>
  145. <th>Location</th>
  146. <th>Established</th>
  147. </tr>
  148. </thead>
  149. <tbody>
  150. <tr>
  151. <td>Yellowstone</td>
  152. <td>Montana, Wyoming, Idaho</td>
  153. <td>March 1, 1872</td>
  154. </tr>
  155. <tr>
  156. <td>Yosemite</td>
  157. <td>California</td>
  158. <td>March 1, 1872</td>
  159. </tr>
  160. <tr>
  161. <td>Zion</td>
  162. <td>Utah</td>
  163. <td>November 19, 1919</td>
  164. </tr>
  165. </tbody>
  166. </table>
  167. HTML,
  168. ];
  169. }
  170. }