xref: /webtrees/app/Factories/MarkdownFactory.php (revision 2ebcf907ed34213f816592af04e6c160335d6311)
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\ResponsiveTableExtension;
24use Fisharebest\Webtrees\CommonMark\XrefExtension;
25use Fisharebest\Webtrees\Contracts\MarkdownFactoryInterface;
26use Fisharebest\Webtrees\Tree;
27use League\CommonMark\Block\Element\Document;
28use League\CommonMark\Block\Element\Paragraph;
29use League\CommonMark\Block\Renderer\DocumentRenderer;
30use League\CommonMark\Block\Renderer\ParagraphRenderer;
31use League\CommonMark\CommonMarkConverter;
32use League\CommonMark\Environment;
33use League\CommonMark\Extension\Autolink\AutolinkExtension;
34use League\CommonMark\Inline\Element\Link;
35use League\CommonMark\Inline\Element\Text;
36use League\CommonMark\Inline\Renderer\LinkRenderer;
37use League\CommonMark\Inline\Renderer\TextRenderer;
38
39/**
40 * Create a markdown converter.
41 */
42class MarkdownFactory implements MarkdownFactoryInterface
43{
44    protected const CONFIG = [
45        'allow_unsafe_links' => false,
46        'html_input'         => Environment::HTML_INPUT_ESCAPE,
47    ];
48
49    /**
50     * @param Tree|null $tree
51     *
52     * @return CommonMarkConverter
53     */
54    public function autolink(Tree $tree = null): CommonMarkConverter
55    {
56        // Create a minimal commonmark processor - just add support for auto-links.
57        $environment = new Environment();
58        $environment->addBlockRenderer(Document::class, new DocumentRenderer());
59        $environment->addBlockRenderer(Paragraph::class, new ParagraphRenderer());
60        $environment->addInlineRenderer(Text::class, new TextRenderer());
61        $environment->addInlineRenderer(Link::class, new LinkRenderer());
62        $environment->addExtension(new AutolinkExtension());
63
64        // Optionally create links to other records.
65        if ($tree instanceof Tree) {
66            $environment->addExtension(new XrefExtension($tree));
67        }
68
69        return new CommonMarkConverter(static::CONFIG, $environment);
70    }
71
72    /**
73     * @param Tree|null $tree
74     *
75     * @return CommonMarkConverter
76     */
77    public function markdown(Tree $tree = null): CommonMarkConverter
78    {
79        $environment = Environment::createCommonMarkEnvironment();
80
81        // Wrap tables so support horizontal scrolling with bootstrap.
82        $environment->addExtension(new ResponsiveTableExtension());
83
84        // Convert webtrees 1.x style census tables to commonmark format.
85        $environment->addExtension(new CensusTableExtension());
86
87        // Optionally create links to other records.
88        if ($tree instanceof Tree) {
89            $environment->addExtension(new XrefExtension($tree));
90        }
91
92        return new CommonMarkConverter(static::CONFIG, $environment);
93    }
94}
95