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