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\ElementRendererInterface;
18use League\CommonMark\Extension\Footnote\Node\FootnoteBackref;
19use League\CommonMark\HtmlElement;
20use League\CommonMark\Inline\Element\AbstractInline;
21use League\CommonMark\Inline\Renderer\InlineRendererInterface;
22use League\CommonMark\Util\ConfigurationAwareInterface;
23use League\CommonMark\Util\ConfigurationInterface;
24
25final class FootnoteBackrefRenderer implements InlineRendererInterface, ConfigurationAwareInterface
26{
27    /** @var ConfigurationInterface */
28    private $config;
29
30    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
31    {
32        if (!($inline instanceof FootnoteBackref)) {
33            throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
34        }
35
36        $attrs = $inline->getData('attributes', []);
37        $attrs['class'] = $attrs['class'] ?? $this->config->get('footnote/backref_class', 'footnote-backref');
38        $attrs['rev'] = 'footnote';
39        $attrs['href'] = \mb_strtolower($inline->getReference()->getDestination());
40        $attrs['role'] = 'doc-backlink';
41
42        return '&nbsp;' . new HtmlElement('a', $attrs, '&#8617;', true);
43    }
44
45    public function setConfiguration(ConfigurationInterface $configuration)
46    {
47        $this->config = $configuration;
48    }
49}
50