1<?php
2
3/*
4 * This file is part of the clockoon/dokuwiki-commonmark-plugin package.
5 *
6 * (c) Sungbin Jeon <clockoon@gmail.com>
7 *
8 * Original code based on the followings:
9 * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane
10 * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com>
11 *
12 * For the full copyright and license information, please view the LICENSE
13 * file that was distributed with this source code.
14 */
15
16declare(strict_types=1);
17
18namespace DokuWiki\Plugin\Commonmark\Extension\Renderer\Inline;
19
20use League\CommonMark\Renderer\ChildNodeRendererInterface;
21use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
22use League\CommonMark\Extension\Footnote\Node\Footnote;
23use League\CommonMark\Node\Node;
24use League\CommonMark\Renderer\NodeRendererInterface;
25use League\Config\ConfigurationAwareInterface;
26use League\Config\ConfigurationInterface;
27
28final class FootnoteRefRenderer implements NodeRendererInterface, ConfigurationAwareInterface
29{
30    /** @var ConfigurationInterface */
31    private ConfigurationInterface $config;
32
33    public function render(Node $node, ChildNodeRendererInterface $DWRenderer)
34    {
35        FootnoteRef::assertInstanceOf($node);
36
37        $attrs = $node->data->getData('attributes');
38
39        # get parents iteratively until get top-level document
40        $document = $node->parent()->parent();
41        while (get_class($document)!='League\CommonMark\Node\Block\Document'){
42            $document = $document->parent();
43        }
44        $walker = $document->walker();
45        $title = $node->getReference()->getLabel();
46
47        while ($event = $walker->next()) {
48            $node = $event->getNode();
49            if ($node instanceof Footnote && $title == $node->getReference()->getLabel()) {
50                $text = $DWRenderer->renderNode($node->children()[0]);
51                break;
52            }
53        }
54
55        $result = '(('. $text. '))';
56        return $result;
57
58    }
59
60    public function setConfiguration(ConfigurationInterface $configuration): void
61    {
62        $this->config = $configuration;
63    }
64}
65