Skip to content

Commit d3e531a

Browse files
authored
Merge pull request #899 from thephpleague/fix-embed-parsing
Fix embed parsing
2 parents da3ce30 + ce60e0d commit d3e531a

File tree

9 files changed

+116
-6
lines changed

9 files changed

+116
-6
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
66

77
## [Unreleased][unreleased]
88

9+
### Changed
10+
11+
- Made a number of small tweaks to the embed extension's parsing behavior to fix #898:
12+
- Changed `EmbedStartParser` to always capture embed-like lines in container blocks, regardless of parent block type
13+
- Changed `EmbedProcessor` to also remove `Embed` blocks that aren't direct children of the `Document`
14+
- Increased the priority of `EmbedProcessor` to `1010`
15+
16+
### Fixed
17+
18+
- Fixed `EmbedExtension` not parsing embeds following a list block (#898)
19+
920
## [2.3.3] - 2022-06-07
1021

1122
### Fixed

src/Extension/Embed/EmbedExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function register(EnvironmentBuilderInterface $environment): void
4242

4343
$environment
4444
->addBlockStartParser(new EmbedStartParser(), 300)
45-
->addEventListener(DocumentParsedEvent::class, new EmbedProcessor($adapter, $environment->getConfiguration()->get('embed.fallback')))
45+
->addEventListener(DocumentParsedEvent::class, new EmbedProcessor($adapter, $environment->getConfiguration()->get('embed.fallback')), 1010)
4646
->addRenderer(Embed::class, new EmbedRenderer());
4747
}
4848
}

src/Extension/Embed/EmbedProcessor.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use League\CommonMark\Event\DocumentParsedEvent;
1717
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
1818
use League\CommonMark\Node\Block\Paragraph;
19+
use League\CommonMark\Node\Inline\Text;
1920
use League\CommonMark\Node\NodeIterator;
2021

2122
final class EmbedProcessor
@@ -34,9 +35,18 @@ public function __construct(EmbedAdapterInterface $adapter, string $fallback = s
3435

3536
public function __invoke(DocumentParsedEvent $event): void
3637
{
37-
$embeds = [];
38-
foreach (new NodeIterator($event->getDocument()) as $node) {
39-
if ($node instanceof Embed) {
38+
$document = $event->getDocument();
39+
$embeds = [];
40+
foreach (new NodeIterator($document) as $node) {
41+
if (! ($node instanceof Embed)) {
42+
continue;
43+
}
44+
45+
if ($node->parent() !== $document) {
46+
$replacement = new Paragraph();
47+
$replacement->appendChild(new Text($node->getUrl()));
48+
$node->replaceWith($replacement);
49+
} else {
4050
$embeds[] = $node;
4151
}
4252
}

src/Extension/Embed/EmbedStartParser.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace League\CommonMark\Extension\Embed;
1515

16-
use League\CommonMark\Node\Block\Document;
1716
use League\CommonMark\Parser\Block\BlockStart;
1817
use League\CommonMark\Parser\Block\BlockStartParserInterface;
1918
use League\CommonMark\Parser\Cursor;
@@ -24,7 +23,7 @@ class EmbedStartParser implements BlockStartParserInterface
2423
{
2524
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
2625
{
27-
if ($cursor->isIndented() || $parserState->getParagraphContent() !== null || ! ($parserState->getLastMatchedBlockParser()->getBlock() instanceof Document)) {
26+
if ($cursor->isIndented() || $parserState->getParagraphContent() !== null || ! ($parserState->getActiveBlockParser()->isContainer())) {
2827
return BlockStart::none();
2928
}
3029

tests/functional/Extension/Embed/EmbedExtensionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use League\CommonMark\ConverterInterface;
1717
use League\CommonMark\Environment\Environment;
18+
use League\CommonMark\Extension\Autolink\AutolinkExtension;
1819
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
1920
use League\CommonMark\Extension\Embed\EmbedExtension;
2021
use League\CommonMark\MarkdownConverter;
@@ -30,10 +31,17 @@ protected function createConverter(array $config = []): ConverterInterface
3031
{
3132
$config['embed']['adapter'] = new FakeAdapter();
3233

34+
$enableAutolinkExtension = $config['enable_autolinks'] ?? false;
35+
unset($config['enable_autolinks']);
36+
3337
$environment = new Environment($config);
3438
$environment->addExtension(new CommonMarkCoreExtension());
3539
$environment->addExtension(new EmbedExtension());
3640

41+
if ($enableAutolinkExtension) {
42+
$environment->addExtension(new AutolinkExtension());
43+
}
44+
3745
return new MarkdownConverter($environment);
3846
}
3947

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<h1>Embed Bug</h1>
2+
<ul>
3+
<li>Both links below</li>
4+
<li>Should be embeds</li>
5+
</ul>
6+
<iframe class="embed" src="https://www.youtube.com/watch?v=dQw4w9WgXcQ"></iframe>
7+
<iframe class="embed" src="https://www.youtube.com/watch?v=dQw4w9WgXcQ"></iframe>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Embed Bug
2+
3+
- Both links below
4+
- Should be embeds
5+
6+
https://www.youtube.com/watch?v=dQw4w9WgXcQ
7+
8+
https://www.youtube.com/watch?v=dQw4w9WgXcQ
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<p>This is an embed:</p>
2+
<iframe class="embed" src="https://www.youtube.com/watch?v=dQw4w9WgXcQ"></iframe>
3+
<p>So is this, with only three spaces of indentation:</p>
4+
<iframe class="embed" src="https://www.youtube.com/watch?v=dQw4w9WgXcQ"></iframe>
5+
<p>This is not, because it's fully indented:</p>
6+
<pre><code>https://www.youtube.com/watch?v=dQw4w9WgXcQ
7+
</code></pre>
8+
<p>This is not, because it has extra bits after the URL:</p>
9+
<p><a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">https://www.youtube.com/watch?v=dQw4w9WgXcQ</a> &lt;-- you gotta watch this!</p>
10+
<p>This is not, because it's in a fenced code block:</p>
11+
<pre><code class="language-md">
12+
https://www.youtube.com/watch?v=dQw4w9WgXcQ
13+
14+
</code></pre>
15+
<p>And this isn't either because it's inline: <img src="https://www.youtube.com/watch?v=dQw4w9WgXcQ" alt="" /></p>
16+
<p>Embeds can't be nested in other blocks:</p>
17+
<ul>
18+
<li>https://www.youtube.com/watch?v=dQw4w9WgXcQ
19+
<ul>
20+
<li>https://www.youtube.com/watch?v=dQw4w9WgXcQ</li>
21+
</ul>
22+
</li>
23+
</ul>
24+
<p>This isn't valid because it's a lazy paragraph continuation:
25+
<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">https://www.youtube.com/watch?v=dQw4w9WgXcQ</a></p>
26+
<iframe class="embed" src="https://www.youtube.com/watch?v=dQw4w9WgXcQ"></iframe>
27+
<p>^ This is fine, though</p>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
enable_autolinks: true
3+
---
4+
5+
This is an embed:
6+
7+
https://www.youtube.com/watch?v=dQw4w9WgXcQ
8+
9+
So is this, with only three spaces of indentation:
10+
11+
https://www.youtube.com/watch?v=dQw4w9WgXcQ
12+
13+
This is not, because it's fully indented:
14+
15+
https://www.youtube.com/watch?v=dQw4w9WgXcQ
16+
17+
This is not, because it has extra bits after the URL:
18+
19+
https://www.youtube.com/watch?v=dQw4w9WgXcQ <-- you gotta watch this!
20+
21+
This is not, because it's in a fenced code block:
22+
23+
```md
24+
25+
https://www.youtube.com/watch?v=dQw4w9WgXcQ
26+
27+
```
28+
29+
And this isn't either because it's inline: ![](https://www.youtube.com/watch?v=dQw4w9WgXcQ)
30+
31+
Embeds can't be nested in other blocks:
32+
33+
- https://www.youtube.com/watch?v=dQw4w9WgXcQ
34+
- https://www.youtube.com/watch?v=dQw4w9WgXcQ
35+
36+
This isn't valid because it's a lazy paragraph continuation:
37+
https://www.youtube.com/watch?v=dQw4w9WgXcQ
38+
39+
https://www.youtube.com/watch?v=dQw4w9WgXcQ
40+
^ This is fine, though

0 commit comments

Comments
 (0)