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
|
|
|
|
22
|
|
|
class MarkdownConverter implements MarkdownConverterInterface |
23
|
|
|
{ |
24
|
|
|
/** @var EnvironmentInterface */ |
25
|
|
|
private $environment; |
26
|
|
|
|
27
|
|
|
/** @var MarkdownParserInterface */ |
28
|
|
|
private $markdownParser; |
29
|
|
|
|
30
|
|
|
/** @var HtmlRenderer */ |
31
|
|
|
private $htmlRenderer; |
32
|
|
|
|
33
|
2850 |
|
public function __construct(EnvironmentInterface $environment) |
34
|
|
|
{ |
35
|
2850 |
|
$this->environment = $environment; |
36
|
2850 |
|
$this->markdownParser = new MarkdownParser($environment); |
37
|
2850 |
|
$this->htmlRenderer = new HtmlRenderer($environment); |
38
|
2850 |
|
} |
39
|
|
|
|
40
|
15 |
|
public function getEnvironment(): EnvironmentInterface |
41
|
|
|
{ |
42
|
15 |
|
return $this->environment; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Converts CommonMark to HTML. |
47
|
|
|
* |
48
|
|
|
* @param string $markdown The Markdown to convert |
49
|
|
|
* |
50
|
|
|
* @return RenderedContentInterface Rendered HTML |
51
|
|
|
* |
52
|
|
|
* @throws \RuntimeException |
53
|
|
|
*/ |
54
|
2835 |
|
public function convertToHtml(string $markdown): RenderedContentInterface |
55
|
|
|
{ |
56
|
2835 |
|
$documentAST = $this->markdownParser->parse($markdown); |
57
|
|
|
|
58
|
2814 |
|
return $this->htmlRenderer->renderDocument($documentAST); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Converts CommonMark to HTML. |
63
|
|
|
* |
64
|
|
|
* @see Converter::convertToHtml |
65
|
|
|
* |
66
|
|
|
* @throws \RuntimeException |
67
|
|
|
*/ |
68
|
6 |
|
public function __invoke(string $markdown): RenderedContentInterface |
69
|
|
|
{ |
70
|
6 |
|
return $this->convertToHtml($markdown); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|