xref: /webtrees/app/Factories/MarkdownFactory.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
14d35caa7SGreg Roach<?php
24d35caa7SGreg Roach
34d35caa7SGreg Roach/**
44d35caa7SGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
64d35caa7SGreg Roach * This program is free software: you can redistribute it and/or modify
74d35caa7SGreg Roach * it under the terms of the GNU General Public License as published by
84d35caa7SGreg Roach * the Free Software Foundation, either version 3 of the License, or
94d35caa7SGreg Roach * (at your option) any later version.
104d35caa7SGreg Roach * This program is distributed in the hope that it will be useful,
114d35caa7SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124d35caa7SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134d35caa7SGreg Roach * GNU General Public License for more details.
144d35caa7SGreg Roach * You should have received a copy of the GNU General Public License
154d35caa7SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
164d35caa7SGreg Roach */
174d35caa7SGreg Roach
184d35caa7SGreg Roachdeclare(strict_types=1);
194d35caa7SGreg Roach
204d35caa7SGreg Roachnamespace Fisharebest\Webtrees\Factories;
214d35caa7SGreg Roach
224d35caa7SGreg Roachuse Fisharebest\Webtrees\CommonMark\CensusTableExtension;
234d35caa7SGreg Roachuse Fisharebest\Webtrees\CommonMark\XrefExtension;
244d35caa7SGreg Roachuse Fisharebest\Webtrees\Contracts\MarkdownFactoryInterface;
254d35caa7SGreg Roachuse Fisharebest\Webtrees\Tree;
266f595250SGreg Roachuse League\CommonMark\Environment\Environment;
274d35caa7SGreg Roachuse League\CommonMark\Extension\Autolink\AutolinkExtension;
286f595250SGreg Roachuse League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
296f595250SGreg Roachuse League\CommonMark\Extension\CommonMark\Node\Inline\Link;
306f595250SGreg Roachuse League\CommonMark\Extension\CommonMark\Renderer\Inline\LinkRenderer;
316f595250SGreg Roachuse League\CommonMark\Extension\Table\TableExtension;
326f595250SGreg Roachuse League\CommonMark\MarkdownConverter;
336f595250SGreg Roachuse League\CommonMark\Node\Block\Document;
346f595250SGreg Roachuse League\CommonMark\Node\Block\Paragraph;
356f595250SGreg Roachuse League\CommonMark\Node\Inline\Text;
366f595250SGreg Roachuse League\CommonMark\Renderer\Block\DocumentRenderer;
376f595250SGreg Roachuse League\CommonMark\Renderer\Block\ParagraphRenderer;
386f595250SGreg Roachuse League\CommonMark\Renderer\Inline\TextRenderer;
396f595250SGreg Roachuse League\CommonMark\Util\HtmlFilter;
404d35caa7SGreg Roach
414d35caa7SGreg Roach/**
424d35caa7SGreg Roach * Create a markdown converter.
434d35caa7SGreg Roach */
444d35caa7SGreg Roachclass MarkdownFactory implements MarkdownFactoryInterface
454d35caa7SGreg Roach{
466f595250SGreg Roach    protected const CONFIG_AUTOLINK = [
474d35caa7SGreg Roach        'allow_unsafe_links' => false,
486f595250SGreg Roach        'html_input'         => HtmlFilter::ESCAPE,
496f595250SGreg Roach    ];
506f595250SGreg Roach
516f595250SGreg Roach    protected const CONFIG_MARKDOWN = [
526f595250SGreg Roach        'allow_unsafe_links' => false,
536f595250SGreg Roach        'html_input'         => HtmlFilter::ESCAPE,
546f595250SGreg Roach        'table'              => [
556f595250SGreg Roach            'wrap' => [
566f595250SGreg Roach                'enabled'    => true,
576f595250SGreg Roach                'tag'        => 'div',
586f595250SGreg Roach                'attributes' => [
596f595250SGreg Roach                    'class' => 'table-responsive',
606f595250SGreg Roach                ],
616f595250SGreg Roach            ],
626f595250SGreg Roach        ],
634d35caa7SGreg Roach    ];
644d35caa7SGreg Roach
654d35caa7SGreg Roach    /**
666f595250SGreg Roach     * @param string    $markdown
674d35caa7SGreg Roach     * @param Tree|null $tree
684d35caa7SGreg Roach     *
696f595250SGreg Roach     * @return string
704d35caa7SGreg Roach     */
716f595250SGreg Roach    public function autolink(string $markdown, Tree $tree = null): string
724d35caa7SGreg Roach    {
734d35caa7SGreg Roach        // Create a minimal commonmark processor - just add support for auto-links.
746f595250SGreg Roach        $environment = new Environment(static::CONFIG_AUTOLINK);
756f595250SGreg Roach        $environment->addRenderer(Document::class, new DocumentRenderer());
766f595250SGreg Roach        $environment->addRenderer(Paragraph::class, new ParagraphRenderer());
776f595250SGreg Roach        $environment->addRenderer(Text::class, new TextRenderer());
786f595250SGreg Roach        $environment->addRenderer(Link::class, new LinkRenderer());
794d35caa7SGreg Roach        $environment->addExtension(new AutolinkExtension());
804d35caa7SGreg Roach
814d35caa7SGreg Roach        // Optionally create links to other records.
824d35caa7SGreg Roach        if ($tree instanceof Tree) {
834d35caa7SGreg Roach            $environment->addExtension(new XrefExtension($tree));
844d35caa7SGreg Roach        }
854d35caa7SGreg Roach
866f595250SGreg Roach        $converter = new MarkDownConverter($environment);
876f595250SGreg Roach
886f595250SGreg Roach        return $converter->convert($markdown)->getContent();
894d35caa7SGreg Roach    }
904d35caa7SGreg Roach
914d35caa7SGreg Roach    /**
926f595250SGreg Roach     * @param string    $markdown
934d35caa7SGreg Roach     * @param Tree|null $tree
944d35caa7SGreg Roach     *
956f595250SGreg Roach     * @return string
964d35caa7SGreg Roach     */
976f595250SGreg Roach    public function markdown(string $markdown, Tree $tree = null): string
984d35caa7SGreg Roach    {
996f595250SGreg Roach        $environment = new Environment(static::CONFIG_MARKDOWN);
1006f595250SGreg Roach        $environment->addExtension(new CommonMarkCoreExtension());
1016f595250SGreg Roach        $environment->addExtension(new TableExtension());
1024d35caa7SGreg Roach
1034d35caa7SGreg Roach        // Convert webtrees 1.x style census tables to commonmark format.
1047a2a7291SGreg Roach        $environment->addExtension(new CensusTableExtension());
1054d35caa7SGreg Roach
1064d35caa7SGreg Roach        // Optionally create links to other records.
1074d35caa7SGreg Roach        if ($tree instanceof Tree) {
1084d35caa7SGreg Roach            $environment->addExtension(new XrefExtension($tree));
1094d35caa7SGreg Roach        }
1104d35caa7SGreg Roach
1116f595250SGreg Roach        $converter = new MarkDownConverter($environment);
1126f595250SGreg Roach
1136f595250SGreg Roach        return $converter->convert($markdown)->getContent();
1144d35caa7SGreg Roach    }
1154d35caa7SGreg Roach}
116