1<?php
2
3/*
4 * This file is part of the league/commonmark package.
5 *
6 * (c) Colin O'Dell <colinodell@gmail.com>
7 * (c) Rezo Zero / Ambroise Maupate
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13declare(strict_types=1);
14
15namespace League\CommonMark\Extension\Footnote\Renderer;
16
17use League\CommonMark\Block\Element\AbstractBlock;
18use League\CommonMark\Block\Renderer\BlockRendererInterface;
19use League\CommonMark\ElementRendererInterface;
20use League\CommonMark\Extension\Footnote\Node\Footnote;
21use League\CommonMark\HtmlElement;
22use League\CommonMark\Util\ConfigurationAwareInterface;
23use League\CommonMark\Util\ConfigurationInterface;
24
25final class FootnoteRenderer implements BlockRendererInterface, ConfigurationAwareInterface
26{
27    /** @var ConfigurationInterface */
28    private $config;
29
30    /**
31     * @param Footnote                 $block
32     * @param ElementRendererInterface $htmlRenderer
33     * @param bool                     $inTightList
34     *
35     * @return HtmlElement
36     */
37    public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false)
38    {
39        if (!($block instanceof Footnote)) {
40            throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
41        }
42
43        $attrs = $block->getData('attributes', []);
44        $attrs['class'] = $attrs['class'] ?? $this->config->get('footnote/footnote_class', 'footnote');
45        $attrs['id'] = $this->config->get('footnote/footnote_id_prefix', 'fn:') . \mb_strtolower($block->getReference()->getLabel());
46        $attrs['role'] = 'doc-endnote';
47
48        foreach ($block->getBackrefs() as $backref) {
49            $block->lastChild()->appendChild($backref);
50        }
51
52        return new HtmlElement(
53            'li',
54            $attrs,
55            $htmlRenderer->renderBlocks($block->children()),
56            true
57        );
58    }
59
60    public function setConfiguration(ConfigurationInterface $configuration)
61    {
62        $this->config = $configuration;
63    }
64}
65