Completed
Pull Request — master (#14)
by ARCANEDEV
04:51
created

MarkdownParser   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 47
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10
wmc 2
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A convertToHtml() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMarkdown;
6
7
use Arcanedev\LaravelMarkdown\Contracts\Parser;
8
use League\CommonMark\CommonMarkConverter;
9
use League\CommonMark\Environment;
10
use League\CommonMark\Ext\Table\TableExtension;
11
12
/**
13
 * Class     MarkdownParser
14
 *
15
 * @package  Arcanedev\LaravelMarkdown\Parsers
16
 * @author   ARCANEDEV <[email protected]>
17
 */
18
class MarkdownParser implements Parser
19
{
20
    /* -----------------------------------------------------------------
21
     |  Properties
22
     | -----------------------------------------------------------------
23
     */
24
25
    /**
26
     * Parser Options.
27
     *
28
     * @var array
29
     */
30
    private $options;
31
32
    /* -----------------------------------------------------------------
33
     |  Constructor
34
     | -----------------------------------------------------------------
35
     */
36
37 16
    public function __construct(array $options)
38
    {
39
40 16
        $this->options = $options;
41 16
    }
42
43
    /* -----------------------------------------------------------------
44
     |  Main Methods
45
     | -----------------------------------------------------------------
46
     */
47
48
    /**
49
     * Convert the given Markdown text into HTML.
50
     *
51
     * @param  string  $text
52
     *
53
     * @return string
54
     */
55 12
    public function convertToHtml(string $text): string
56
    {
57 12
        $environment = tap(Environment::createCommonMarkEnvironment())
58 12
            ->addExtension(new TableExtension);
59
60 12
        $converter = new CommonMarkConverter($this->options, $environment);
61
62 12
        return $converter->convertToHtml($text);
63
    }
64
}
65