|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Eightfold\CommonMarkAbbreviations\Tests; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +use League\CommonMark\Environment; |
| 8 | +use League\CommonMark\CommonMarkConverter; |
| 9 | +use League\CommonMark\Event\DocumentParsedEvent; |
| 10 | +use League\CommonMark\Extension\ExternalLink\ExternalLinkExtension; |
| 11 | + |
| 12 | +use Eightfold\Shoop\Shoop; |
| 13 | + |
| 14 | +use Eightfold\CommonMarkAbbreviations\Abbreviation; |
| 15 | +use Eightfold\CommonMarkAbbreviations\AbbreviationExtension; |
| 16 | + |
| 17 | +class AbbreviationAttributesTest extends TestCase |
| 18 | +{ |
| 19 | + public function testParser() |
| 20 | + { |
| 21 | + $environment = Environment::createCommonMarkEnvironment(); |
| 22 | + $environment->addExtension(new AbbreviationExtension()); |
| 23 | + $environment->addExtension(new ExternalLinkExtension()); |
| 24 | + $environment->addEventListener(DocumentParsedEvent::class, function ( |
| 25 | + DocumentParsedEvent $event |
| 26 | + ) { |
| 27 | + $document = $event->getDocument(); |
| 28 | + $walker = $document->walker(); |
| 29 | + while ($event = $walker->next()) { |
| 30 | + $node = $event->getNode(); |
| 31 | + |
| 32 | + // Ignore any nodes that aren't Abbreviation nodes, and only act |
| 33 | + // when we first encounter/enter an Abbreviation node. |
| 34 | + if (!($node instanceof Abbreviation) || !$event->isEntering()) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + // Add a test attribute. It's also possible to alter the |
| 39 | + // existing attributes here. |
| 40 | + $node->data['attributes']['data-event-attribute'] = 'hello'; |
| 41 | + } |
| 42 | + }); |
| 43 | + $converter = new CommonMarkConverter([ |
| 44 | + "external_link" => ["open_in_new_window" => true] |
| 45 | + ], $environment); |
| 46 | + |
| 47 | + $path = Shoop::this(__DIR__)->append("/short-doc-attributes.md"); |
| 48 | + $markdown = file_get_contents($path); |
| 49 | + $expected = '<p><abbr title="United States Web Design System" data-inline-attribute="hello" data-event-attribute="hello">USWDS</abbr></p>'."\n".'<p><a rel="noopener noreferrer" target="_blank" href="https://8fold.pro">External link check</a></p>'."\n"; |
| 50 | + $actual = $converter->convertToHtml($markdown); |
| 51 | + $this->assertEquals($expected, $actual); |
| 52 | + |
| 53 | + $path = Shoop::this(__DIR__)->divide("/") |
| 54 | + ->dropLast()->append(["readme.html"])->asString("/"); |
| 55 | + $expected = file_get_contents($path); |
| 56 | + |
| 57 | + $path = Shoop::this(__DIR__)->divide("/") |
| 58 | + ->dropLast()->append(["README.md"])->asString("/"); |
| 59 | + $markdown = file_get_contents($path); |
| 60 | + |
| 61 | + $actual = $converter->convertToHtml($markdown); |
| 62 | + $this->assertEquals($expected, $actual); |
| 63 | + } |
| 64 | +} |
0 commit comments