xref: /webtrees/app/Factories/MarkdownFactory.php (revision 89950eb4a8b302be0915d2ba9885d70db994abd6)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Factories;
21
22use Fisharebest\Webtrees\CommonMark\CensusTableExtension;
23use Fisharebest\Webtrees\CommonMark\XrefExtension;
24use Fisharebest\Webtrees\Contracts\MarkdownFactoryInterface;
25use Fisharebest\Webtrees\Tree;
26use League\CommonMark\Environment\Environment;
27use League\CommonMark\Extension\Autolink\AutolinkExtension;
28use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
29use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
30use League\CommonMark\Extension\CommonMark\Renderer\Inline\LinkRenderer;
31use League\CommonMark\Extension\Table\TableExtension;
32use League\CommonMark\MarkdownConverter;
33use League\CommonMark\Node\Block\Document;
34use League\CommonMark\Node\Block\Paragraph;
35use League\CommonMark\Node\Inline\Text;
36use League\CommonMark\Renderer\Block\DocumentRenderer;
37use League\CommonMark\Renderer\Block\ParagraphRenderer;
38use League\CommonMark\Renderer\Inline\TextRenderer;
39use League\CommonMark\Util\HtmlFilter;
40
41/**
42 * Create a markdown converter.
43 */
44class MarkdownFactory implements MarkdownFactoryInterface
45{
46    protected const CONFIG_AUTOLINK = [
47        'allow_unsafe_links' => false,
48        'html_input'         => HtmlFilter::ESCAPE,
49    ];
50
51    protected const CONFIG_MARKDOWN = [
52        'allow_unsafe_links' => false,
53        'html_input'         => HtmlFilter::ESCAPE,
54        'table'              => [
55            'wrap' => [
56                'enabled'    => true,
57                'tag'        => 'div',
58                'attributes' => [
59                    'class' => 'table-responsive',
60                ],
61            ],
62        ],
63    ];
64
65    /**
66     * @param string    $markdown
67     * @param Tree|null $tree
68     *
69     * @return string
70     */
71    public function autolink(string $markdown, Tree $tree = null): string
72    {
73        // Create a minimal commonmark processor - just add support for auto-links.
74        $environment = new Environment(static::CONFIG_AUTOLINK);
75        $environment->addRenderer(Document::class, new DocumentRenderer());
76        $environment->addRenderer(Paragraph::class, new ParagraphRenderer());
77        $environment->addRenderer(Text::class, new TextRenderer());
78        $environment->addRenderer(Link::class, new LinkRenderer());
79        $environment->addExtension(new AutolinkExtension());
80
81        // Optionally create links to other records.
82        if ($tree instanceof Tree) {
83            $environment->addExtension(new XrefExtension($tree));
84        }
85
86        $converter = new MarkDownConverter($environment);
87
88        return $converter->convert($markdown)->getContent();
89    }
90
91    /**
92     * @param string    $markdown
93     * @param Tree|null $tree
94     *
95     * @return string
96     */
97    public function markdown(string $markdown, Tree $tree = null): string
98    {
99        $environment = new Environment(static::CONFIG_MARKDOWN);
100        $environment->addExtension(new CommonMarkCoreExtension());
101        $environment->addExtension(new TableExtension());
102
103        // Convert webtrees 1.x style census tables to commonmark format.
104        $environment->addExtension(new CensusTableExtension());
105
106        // Optionally create links to other records.
107        if ($tree instanceof Tree) {
108            $environment->addExtension(new XrefExtension($tree));
109        }
110
111        $converter = new MarkDownConverter($environment);
112
113        return $converter->convert($markdown)->getContent();
114    }
115}
116