Skip to content

Commit 77e6c2e

Browse files
authored
Merge pull request #6 from Ambient-Impact/attributes
Added support for attributes on the Abbreviation element.
2 parents 4075752 + 36bee8d commit 77e6c2e

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

src/Abbreviation.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Eightfold\CommonMarkAbbreviations;
44

55
use League\CommonMark\Inline\Element\AbstractInline;
6+
use League\CommonMark\HtmlElement;
67

78
class Abbreviation extends AbstractInline
89
{
@@ -22,6 +23,10 @@ public function isContainer(): bool
2223

2324
public function element()
2425
{
25-
return '<abbr title="'. $this->title .'">'. $this->abbr .'</abbr>';
26+
$attributes = $this->getData('attributes', []);
27+
28+
$attributes['title'] = $this->title;
29+
30+
return new HtmlElement('abbr', $attributes, $this->abbr);
2631
}
2732
}

tests/AbbreviationAttributesTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

tests/short-doc-attributes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[.USWDS](United States Web Design System){data-inline-attribute="hello"}
2+
3+
[External link check](https://8fold.pro)

0 commit comments

Comments
 (0)