1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the league/commonmark package.
7 *
8 * (c) Colin O'Dell <colinodell@gmail.com>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14namespace League\CommonMark\Extension\ExternalLink;
15
16use League\CommonMark\Environment\EnvironmentBuilderInterface;
17use League\CommonMark\Event\DocumentParsedEvent;
18use League\CommonMark\Extension\ConfigurableExtensionInterface;
19use League\Config\ConfigurationBuilderInterface;
20use Nette\Schema\Expect;
21
22final class ExternalLinkExtension implements ConfigurableExtensionInterface
23{
24    public function configureSchema(ConfigurationBuilderInterface $builder): void
25    {
26        $applyOptions = [
27            ExternalLinkProcessor::APPLY_NONE,
28            ExternalLinkProcessor::APPLY_ALL,
29            ExternalLinkProcessor::APPLY_INTERNAL,
30            ExternalLinkProcessor::APPLY_EXTERNAL,
31        ];
32
33        $builder->addSchema('external_link', Expect::structure([
34            'internal_hosts' => Expect::type('string|string[]'),
35            'open_in_new_window' => Expect::bool(false),
36            'html_class' => Expect::string()->default(''),
37            'nofollow' => Expect::anyOf(...$applyOptions)->default(ExternalLinkProcessor::APPLY_NONE),
38            'noopener' => Expect::anyOf(...$applyOptions)->default(ExternalLinkProcessor::APPLY_EXTERNAL),
39            'noreferrer' => Expect::anyOf(...$applyOptions)->default(ExternalLinkProcessor::APPLY_EXTERNAL),
40        ]));
41    }
42
43    public function register(EnvironmentBuilderInterface $environment): void
44    {
45        $environment->addEventListener(DocumentParsedEvent::class, new ExternalLinkProcessor($environment->getConfiguration()), -50);
46    }
47}
48