1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\Helpers; |
10
|
|
|
|
11
|
|
|
use League\CommonMark\Environment; |
12
|
|
|
use League\CommonMark\Extension\Attributes\AttributesExtension; |
13
|
|
|
use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension; |
14
|
|
|
use League\CommonMark\MarkdownConverter; |
15
|
|
|
|
16
|
|
|
class MarkdownRenderingHelper |
17
|
|
|
{ |
18
|
|
|
private $config = [ |
19
|
|
|
'html_input' => 'escape', |
20
|
|
|
'allow_unsafe_links' => false, |
21
|
|
|
'max_nesting_level' => 10 |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
private $blockRenderer; |
25
|
|
|
private $inlineRenderer; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$blockEnvironment = Environment::createCommonMarkEnvironment(); |
30
|
|
|
$blockEnvironment->addExtension(new AttributesExtension()); |
31
|
|
|
$blockEnvironment->mergeConfig($this->config); |
32
|
|
|
$this->blockRenderer = new MarkdownConverter($blockEnvironment); |
33
|
|
|
|
34
|
|
|
$inlineEnvironment = new Environment(); |
35
|
|
|
$inlineEnvironment->addExtension(new AttributesExtension()); |
36
|
|
|
$inlineEnvironment->addExtension(new InlinesOnlyExtension()); |
37
|
|
|
$inlineEnvironment->mergeConfig($this->config); |
38
|
|
|
$this->inlineRenderer = new MarkdownConverter($inlineEnvironment); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function doRender(string $content): string { |
42
|
|
|
return $this->blockRenderer->convertToHtml($content); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function doRenderInline(string $content): string { |
46
|
|
|
return $this->inlineRenderer->convertToHtml($content); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
} |