1<?php
2
3/*
4 * This file is part of the league/commonmark package.
5 *
6 * (c) Colin O'Dell <colinodell@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace League\CommonMark\Extension\TableOfContents;
13
14use League\CommonMark\Block\Parser\BlockParserInterface;
15use League\CommonMark\ContextInterface;
16use League\CommonMark\Cursor;
17use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
18use League\CommonMark\Util\ConfigurationAwareInterface;
19use League\CommonMark\Util\ConfigurationInterface;
20
21final class TableOfContentsPlaceholderParser implements BlockParserInterface, ConfigurationAwareInterface
22{
23    /** @var ConfigurationInterface */
24    private $config;
25
26    public function parse(ContextInterface $context, Cursor $cursor): bool
27    {
28        $placeholder = $this->config->get('table_of_contents/placeholder');
29        if ($placeholder === null) {
30            return false;
31        }
32
33        // The placeholder must be the only thing on the line
34        if ($cursor->match('/^' . \preg_quote($placeholder, '/') . '$/') === null) {
35            return false;
36        }
37
38        $context->addBlock(new TableOfContentsPlaceholder());
39
40        return true;
41    }
42
43    public function setConfiguration(ConfigurationInterface $configuration)
44    {
45        $this->config = $configuration;
46    }
47}
48