Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
SetsUpMarkdownConverter
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
8 / 8
16
100.00% covered (success)
100.00%
1 / 1
 enableDynamicExtensions
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 enableConfigDefinedExtensions
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 mergeMarkdownConfiguration
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 initializeExtension
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 registerPreProcessors
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 registerPostProcessors
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 registerPreProcessor
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 registerPostProcessor
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Concerns\Internal;
6
7use Hyde\Facades\Config;
8use Hyde\Markdown\Processing\BladeDownProcessor;
9use Hyde\Markdown\Processing\ShortcodeProcessor;
10use Hyde\Markdown\Processing\CodeblockFilepathProcessor;
11use Torchlight\Commonmark\V2\TorchlightExtension;
12
13use function array_merge;
14use function in_array;
15
16/**
17 * Sets up the Markdown converter for the Markdown service.
18 *
19 * @internal This trait is not covered by the backward compatibility promise.
20 *
21 * @see \Hyde\Framework\Services\MarkdownService
22 */
23trait SetsUpMarkdownConverter
24{
25    protected function enableDynamicExtensions(): void
26    {
27        if ($this->canEnablePermalinks()) {
28            $this->configurePermalinksExtension();
29        }
30
31        if ($this->canEnableTorchlight()) {
32            $this->addExtension(TorchlightExtension::class);
33        }
34
35        if (Config::getBool('markdown.allow_html', false)) {
36            $this->enableAllHtmlElements();
37        }
38    }
39
40    protected function enableConfigDefinedExtensions(): void
41    {
42        foreach (Config::getArray('markdown.extensions', []) as $extensionClassName) {
43            $this->addExtension($extensionClassName);
44        }
45    }
46
47    protected function mergeMarkdownConfiguration(): void
48    {
49        $this->config = array_merge(Config::getArray('markdown.config', []), $this->config);
50    }
51
52    public function initializeExtension(string $extensionClassName): void
53    {
54        $this->converter->getEnvironment()->addExtension(new $extensionClassName());
55    }
56
57    protected function registerPreProcessors(): void
58    {
59        $this->registerPreProcessor(BladeDownProcessor::class, Config::getBool('markdown.enable_blade', false));
60
61        $this->registerPreProcessor(ShortcodeProcessor::class);
62        $this->registerPreProcessor(CodeblockFilepathProcessor::class);
63    }
64
65    protected function registerPostProcessors(): void
66    {
67        $this->registerPostProcessor(
68            BladeDownProcessor::class,
69            Config::getBool('markdown.enable_blade', false)
70        );
71
72        $this->registerPostProcessor(
73            CodeblockFilepathProcessor::class,
74            Config::getBool('markdown.features.codeblock_filepaths', true)
75        );
76    }
77
78    protected function registerPreProcessor(string $class, bool $when = true): void
79    {
80        if (! in_array($class, $this->preprocessors) && $when) {
81            $this->preprocessors[] = $class;
82        }
83    }
84
85    protected function registerPostProcessor(string $class, bool $when = true): void
86    {
87        if (! in_array($class, $this->postprocessors) && $when) {
88            $this->postprocessors[] = $class;
89        }
90    }
91}