1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Libraries\Markdown\Osu;
7
8use App\Libraries\Markdown\Attributes\AttributesAllowedListener;
9use League\CommonMark\Environment\EnvironmentBuilderInterface;
10use League\CommonMark\Event\DocumentParsedEvent;
11use League\CommonMark\Extension\Attributes\AttributesExtension;
12use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
13use League\CommonMark\Extension\ConfigurableExtensionInterface;
14use League\CommonMark\Extension\Table\Table;
15use League\Config\ConfigurationBuilderInterface;
16use Nette\Schema\Expect;
17
18class Extension implements ConfigurableExtensionInterface
19{
20 /**
21 * @var DocumentProcessor|null
22 */
23 public $processor;
24
25 public function configureSchema(ConfigurationBuilderInterface $builder): void
26 {
27 $builder->addSchema('osu_extension', Expect::structure([
28 'attributes_allowed' => Expect::arrayOf('string')->nullable(),
29 'block_name' => Expect::string(),
30 'custom_container_inline' => Expect::bool(),
31 'fix_wiki_url' => Expect::bool(),
32 'generate_toc' => Expect::bool(),
33 'record_first_image' => Expect::bool(),
34 'relative_url_root' => Expect::string()->nullable(),
35 'style_block_allowed_classes' => Expect::array()->nullable(),
36 'title_from_document' => Expect::bool(),
37 'wiki_locale' => Expect::string()->nullable(),
38 'with_gallery' => Expect::bool(),
39 ]));
40 }
41
42 public function register(EnvironmentBuilderInterface $environment): void
43 {
44 $this->processor = new DocumentProcessor($environment);
45
46 $environment
47 ->addRenderer(ListItem::class, new Renderers\ListItemRenderer(), 10)
48 ->addRenderer(Table::class, new Renderers\TableRenderer(), 10)
49 // This needs to be run after AttributesExtension so it gets
50 // correct node id attribute for table of contents.
51 ->addEventListener(DocumentParsedEvent::class, $this->processor, -10);
52
53 if ($environment->getConfiguration()->exists('osu_extension/attributes_allowed')) {
54 $environment->addEventListener(DocumentParsedEvent::class, new AttributesAllowedListener());
55 $environment->addExtension(new AttributesExtension());
56 }
57 }
58}