|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the league/commonmark package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace League\CommonMark\Extension\Embed; |
|
15
|
|
|
|
|
16
|
|
|
use League\CommonMark\Environment\EnvironmentBuilderInterface; |
|
17
|
|
|
use League\CommonMark\Event\DocumentParsedEvent; |
|
18
|
|
|
use League\CommonMark\Extension\ConfigurableExtensionInterface; |
|
19
|
|
|
use League\Config\ConfigurationBuilderInterface; |
|
20
|
|
|
use Nette\Schema\Expect; |
|
21
|
|
|
|
|
22
|
|
|
final class EmbedExtension implements ConfigurableExtensionInterface |
|
23
|
|
|
{ |
|
24
|
8 |
|
public function configureSchema(ConfigurationBuilderInterface $builder): void |
|
25
|
|
|
{ |
|
26
|
8 |
|
$builder->addSchema('embed', Expect::structure([ |
|
27
|
8 |
|
'adapter' => Expect::type(EmbedAdapterInterface::class), |
|
28
|
8 |
|
'allowed_domains' => Expect::arrayOf('string')->default([]), |
|
29
|
8 |
|
'fallback' => Expect::anyOf('link', 'remove')->default('link'), |
|
30
|
|
|
])); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
8 |
|
public function register(EnvironmentBuilderInterface $environment): void |
|
34
|
|
|
{ |
|
35
|
8 |
|
$adapter = $environment->getConfiguration()->get('embed.adapter'); |
|
36
|
|
|
\assert($adapter instanceof EmbedAdapterInterface); |
|
37
|
|
|
|
|
38
|
8 |
|
$allowedDomains = $environment->getConfiguration()->get('embed.allowed_domains'); |
|
39
|
8 |
|
if ($allowedDomains !== []) { |
|
40
|
4 |
|
$adapter = new DomainFilteringAdapter($adapter, $allowedDomains); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$environment |
|
44
|
8 |
|
->addBlockStartParser(new EmbedStartParser(), 300) |
|
45
|
8 |
|
->addEventListener(DocumentParsedEvent::class, new EmbedProcessor($adapter, $environment->getConfiguration()->get('embed.fallback'))) |
|
46
|
8 |
|
->addRenderer(Embed::class, new EmbedRenderer()); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|