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