Passed
Push — 2.0 ( 0f9198...c8a95e )
by Greg
06:29
created

MarkdownFactory::autolink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 16
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Factories;
21
22
use Fisharebest\Webtrees\CommonMark\CensusTableExtension;
23
use Fisharebest\Webtrees\CommonMark\ResponsiveTableExtension;
24
use Fisharebest\Webtrees\CommonMark\XrefExtension;
25
use Fisharebest\Webtrees\Contracts\MarkdownFactoryInterface;
26
use Fisharebest\Webtrees\Tree;
27
use League\CommonMark\Block\Element\Document;
28
use League\CommonMark\Block\Element\Paragraph;
29
use League\CommonMark\Block\Renderer\DocumentRenderer;
30
use League\CommonMark\Block\Renderer\ParagraphRenderer;
31
use League\CommonMark\CommonMarkConverter;
32
use League\CommonMark\Environment;
33
use League\CommonMark\EnvironmentInterface;
34
use League\CommonMark\Extension\Autolink\AutolinkExtension;
35
use League\CommonMark\Inline\Element\Link;
36
use League\CommonMark\Inline\Element\Text;
37
use League\CommonMark\Inline\Renderer\LinkRenderer;
38
use League\CommonMark\Inline\Renderer\TextRenderer;
39
40
/**
41
 * Create a markdown converter.
42
 */
43
class MarkdownFactory implements MarkdownFactoryInterface
44
{
45
    protected const CONFIG = [
46
        'allow_unsafe_links' => false,
47
        'html_input'         => EnvironmentInterface::HTML_INPUT_ESCAPE,
48
    ];
49
50
    /**
51
     * @param Tree|null $tree
52
     *
53
     * @return CommonMarkConverter
54
     */
55
    public function autolink(Tree $tree = null): CommonMarkConverter
56
    {
57
        // Create a minimal commonmark processor - just add support for auto-links.
58
        $environment = new Environment();
59
        $environment->addBlockRenderer(Document::class, new DocumentRenderer());
60
        $environment->addBlockRenderer(Paragraph::class, new ParagraphRenderer());
61
        $environment->addInlineRenderer(Text::class, new TextRenderer());
62
        $environment->addInlineRenderer(Link::class, new LinkRenderer());
63
        $environment->addExtension(new AutolinkExtension());
64
65
        // Optionally create links to other records.
66
        if ($tree instanceof Tree) {
67
            $environment->addExtension(new XrefExtension($tree));
68
        }
69
70
        return new CommonMarkConverter(static::CONFIG, $environment);
71
    }
72
73
    /**
74
     * @param Tree|null $tree
75
     *
76
     * @return CommonMarkConverter
77
     */
78
    public function markdown(Tree $tree = null): CommonMarkConverter
79
    {
80
        $environment = Environment::createCommonMarkEnvironment();
81
82
        // Wrap tables so support horizontal scrolling with bootstrap.
83
        $environment->addExtension(new ResponsiveTableExtension());
84
85
        // Convert webtrees 1.x style census tables to commonmark format.
86
        $environment->addExtension(new CensusTableExtension());
87
88
        // Optionally create links to other records.
89
        if ($tree instanceof Tree) {
90
            $environment->addExtension(new XrefExtension($tree));
91
        }
92
93
        return new CommonMarkConverter(static::CONFIG, $environment);
94
    }
95
}
96