xref: /webtrees/app/Factories/MarkdownFactory.php (revision 241d59e1c548aec68ee4908fada64f8c789383eb)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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        'renderer'           => [
50            'soft_break'     => "\n",
51        ],
52    ];
53
54    protected const CONFIG_MARKDOWN = [
55        'allow_unsafe_links' => false,
56        'html_input'         => HtmlFilter::ESCAPE,
57        'renderer'           => [
58            'soft_break'     => "\n",
59        ],
60        'table'              => [
61            'wrap' => [
62                'enabled'    => true,
63                'tag'        => 'div',
64                'attributes' => [
65                    'class' => 'table-responsive',
66                ],
67            ],
68        ],
69    ];
70
71    /**
72     * @param string    $markdown
73     * @param Tree|null $tree
74     *
75     * @return string
76     */
77    public function autolink(string $markdown, Tree $tree = null): string
78    {
79        // Create a minimal commonmark processor - just add support for auto-links.
80        $environment = new Environment(static::CONFIG_AUTOLINK);
81        $environment->addRenderer(Document::class, new DocumentRenderer());
82        $environment->addRenderer(Paragraph::class, new ParagraphRenderer());
83        $environment->addRenderer(Text::class, new TextRenderer());
84        $environment->addRenderer(Link::class, new LinkRenderer());
85        $environment->addExtension(new AutolinkExtension());
86
87        // Optionally create links to other records.
88        if ($tree instanceof Tree) {
89            $environment->addExtension(new XrefExtension($tree));
90        }
91
92        $converter = new MarkDownConverter($environment);
93
94        return $converter->convert($markdown)->getContent();
95    }
96
97    /**
98     * @param string    $markdown
99     * @param Tree|null $tree
100     *
101     * @return string
102     */
103    public function markdown(string $markdown, Tree $tree = null): string
104    {
105        $environment = new Environment(static::CONFIG_MARKDOWN);
106        $environment->addExtension(new CommonMarkCoreExtension());
107        $environment->addExtension(new TableExtension());
108
109        // Convert webtrees 1.x style census tables to commonmark format.
110        $environment->addExtension(new CensusTableExtension());
111
112        // Optionally create links to other records.
113        if ($tree instanceof Tree) {
114            $environment->addExtension(new XrefExtension($tree));
115        }
116
117        $converter = new MarkDownConverter($environment);
118
119        return $converter->convert($markdown)->getContent();
120    }
121}
122