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\ConfigurableEnvironmentInterface;
15use League\CommonMark\Event\DocumentParsedEvent;
16use League\CommonMark\Extension\ExtensionInterface;
17use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
18
19final class TableOfContentsExtension implements ExtensionInterface
20{
21    public function register(ConfigurableEnvironmentInterface $environment): void
22    {
23        $environment->addEventListener(DocumentParsedEvent::class, [new TableOfContentsBuilder(), 'onDocumentParsed'], -150);
24
25        if ($environment->getConfig('table_of_contents/position') === TableOfContentsBuilder::POSITION_PLACEHOLDER) {
26            $environment->addBlockParser(new TableOfContentsPlaceholderParser(), 200);
27            // If a placeholder cannot be replaced with a TOC element this renderer will ensure the parser won't error out
28            $environment->addBlockRenderer(TableOfContentsPlaceholder::class, new TableOfContentsPlaceholderRenderer());
29        }
30    }
31}
32