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\StyleBlock;
7
8use League\CommonMark\Node\Node;
9use League\CommonMark\Renderer\ChildNodeRendererInterface;
10use League\CommonMark\Renderer\NodeRendererInterface;
11use League\CommonMark\Util\HtmlElement;
12use League\Config\ConfigurationAwareInterface;
13use League\Config\ConfigurationInterface;
14
15class Renderer implements NodeRendererInterface, ConfigurationAwareInterface
16{
17 private ConfigurationInterface $config;
18
19 public function setConfiguration(ConfigurationInterface $configuration): void
20 {
21 $this->config = $configuration;
22 }
23
24 public function render(Node $node, ChildNodeRendererInterface $childRenderer)
25 {
26 Element::assertInstanceOf($node);
27
28 $attrs = $node->data->getData('attributes');
29 $className = $node->getClassName();
30
31 $allowedClasses = $this->config->get('osu_extension/style_block_allowed_classes') ?? [];
32 $separator = $this->config->get('renderer/inner_separator') ?? "\n";
33
34 $renderedChildren = $childRenderer->renderNodes($node->children());
35
36 if (!present($className) || !\in_array($className, $allowedClasses, true)) {
37 return $renderedChildren;
38 }
39
40 return new HtmlElement(
41 'div',
42 $attrs->export(),
43 $separator.$renderedChildren.$separator,
44 );
45 }
46}