Skip to content

Commit 96a1b9d

Browse files
authored
Fix inline parsing
1 parent 780aac6 commit 96a1b9d

3 files changed

Lines changed: 14 additions & 32 deletions

File tree

src/Shortcode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Samwilson\CommonMarkShortcodes;
66

7-
use Throwable;
87
use DOMDocument;
98
use League\CommonMark\Node\Block\AbstractBlock;
109
use League\CommonMark\Node\RawMarkupContainerInterface;
10+
use Throwable;
1111

1212
class Shortcode extends AbstractBlock implements RawMarkupContainerInterface
1313
{

src/ShortcodeInlineParser.php

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,21 @@ public function __construct(array $shortcodes)
2323

2424
public function getMatchDefinition(): InlineParserMatch
2525
{
26-
foreach (\array_keys($this->shortcodes) as $code) {
27-
return InlineParserMatch::join(
28-
InlineParserMatch::string('{' . $code),
29-
InlineParserMatch::regex('.*?\}')
30-
);
31-
}
26+
return InlineParserMatch::join(
27+
InlineParserMatch::string('{'),
28+
InlineParserMatch::oneOf(...\array_keys($this->shortcodes)),
29+
InlineParserMatch::regex('.*?'),
30+
InlineParserMatch::string('}')
31+
);
3232
}
3333

3434
public function parse(InlineParserContext $inlineContext): bool
3535
{
36-
$shortcode = new Shortcode(\substr($inlineContext->getMatches()[1], 1));
37-
$shortcode->loadAttrsFromString(\substr($inlineContext->getMatches()[2], 0, -1));
38-
// $doc = new DOMDocument();
39-
// $doc->loadHTML('<body ' . \substr($inlineContext->getMatches()[2], 0, -1) . ' />');
40-
// $body = $doc->getElementsByTagName('body')[0];
41-
// for ($i = 0; $i < $body->attributes->length; ++$i) {
42-
// $name = $body->attributes->item($i)->name;
43-
// $value = $body->getAttribute($name);
44-
// $shortcode->setAttr($name, $value);
45-
// }
36+
$shortcode = new Shortcode($inlineContext->getMatches()[2]);
37+
$shortcode->loadAttrsFromString($inlineContext->getMatches()[3]);
4638
$inlineContext->getCursor()->advanceBy($inlineContext->getFullMatchLength());
4739
$inlineContext->getContainer()->appendChild($shortcode);
4840

4941
return true;
5042
}
51-
52-
// /**
53-
// * @inheritdoc
54-
// */
55-
// public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart {
56-
57-
// $opening = $cursor->match('/^\{([^ ]+)/');
58-
// dump(array_keys($this->shortcodes), $opening, $cursor);
59-
// if ($opening === null) {
60-
// return BlockStart::none();
61-
// }
62-
// return BlockStart::of(new ShortcodeParser(substr($opening, 1)))->at($cursor);
63-
// }
6443
}

tests/ShortcodeExtensionTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ public function provideBasics(): array
3434
{
3535
return [
3636
'simple-inline' => [
37-
'markdown' => 'Foo {bar} baz',
37+
'markdown' => 'Foo {bar} baz {bif}',
3838
'shortcodes' => [
3939
'bar' => static function () {
4040
return 'BAR';
4141
},
42+
'bif' => static function () {
43+
return 'BIF';
44+
},
4245
],
43-
'output' => "<p>Foo \nBAR baz</p>\n",
46+
'output' => "<p>Foo \nBAR baz \nBIF</p>\n",
4447
],
4548
'simple-inline-params' => [
4649
'markdown' => 'Foo {bar a b=c d="e f"} baz',

0 commit comments

Comments
 (0)