1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace League\CommonMark; |
15
|
|
|
|
16
|
|
|
use League\CommonMark\Environment\EnvironmentInterface; |
17
|
|
|
use League\CommonMark\Output\RenderedContentInterface; |
18
|
|
|
use League\CommonMark\Parser\MarkdownParser; |
19
|
|
|
use League\CommonMark\Parser\MarkdownParserInterface; |
20
|
|
|
use League\CommonMark\Renderer\HtmlRenderer; |
21
|
|
|
use League\CommonMark\Renderer\HtmlRendererInterface; |
22
|
|
|
|
23
|
|
|
class MarkdownConverter implements MarkdownConverterInterface |
24
|
|
|
{ |
25
|
|
|
/** @var EnvironmentInterface */ |
26
|
|
|
protected $environment; |
27
|
|
|
|
28
|
|
|
/** @var MarkdownParserInterface */ |
29
|
|
|
protected $markdownParser; |
30
|
|
|
|
31
|
|
|
/** @var HtmlRendererInterface */ |
32
|
|
|
protected $htmlRenderer; |
33
|
|
|
|
34
|
2907 |
|
public function __construct(EnvironmentInterface $environment) |
35
|
|
|
{ |
36
|
2907 |
|
$this->environment = $environment; |
37
|
|
|
|
38
|
2907 |
|
$this->markdownParser = new MarkdownParser($environment); |
39
|
2907 |
|
$this->htmlRenderer = new HtmlRenderer($environment); |
40
|
2907 |
|
} |
41
|
|
|
|
42
|
15 |
|
public function getEnvironment(): EnvironmentInterface |
43
|
|
|
{ |
44
|
15 |
|
return $this->environment; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Converts Markdown to HTML. |
49
|
|
|
* |
50
|
|
|
* @param string $markdown The Markdown to convert |
51
|
|
|
* |
52
|
|
|
* @return RenderedContentInterface Rendered HTML |
53
|
|
|
* |
54
|
|
|
* @throws \RuntimeException |
55
|
|
|
*/ |
56
|
2892 |
|
public function convertToHtml(string $markdown): RenderedContentInterface |
57
|
|
|
{ |
58
|
2892 |
|
$documentAST = $this->markdownParser->parse($markdown); |
59
|
|
|
|
60
|
2871 |
|
return $this->htmlRenderer->renderDocument($documentAST); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Converts CommonMark to HTML. |
65
|
|
|
* |
66
|
|
|
* @see Converter::convertToHtml |
67
|
|
|
* |
68
|
|
|
* @throws \RuntimeException |
69
|
|
|
*/ |
70
|
6 |
|
public function __invoke(string $markdown): RenderedContentInterface |
71
|
|
|
{ |
72
|
6 |
|
return $this->convertToHtml($markdown); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|