xref: /plugin/commonmark/src/Dokuwiki/Plugin/Commonmark/Commonmark.php (revision 78c882e72126658ff199f23f21c6b753ca84258b)
18ec9a8f2SSungbin Jeon<?php
28ec9a8f2SSungbin Jeon
38ec9a8f2SSungbin Jeonnamespace Dokuwiki\Plugin\Commonmark;
48ec9a8f2SSungbin Jeon
58ec9a8f2SSungbin Jeonuse League\CommonMark\Environment;
68ec9a8f2SSungbin Jeonuse League\CommonMark\DocParser;
71e536badSSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\CommonmarkToDokuwikiExtension;
8f46768a8SSungbin Jeonuse Dokuwiki\Plugin\Commonmark\Extension\FootnoteToDokuwikiExtension;
9*78c882e7SSungbin Jeonuse League\CommonMark\Extension\Strikethrough\StrikethroughExtension;
108ec9a8f2SSungbin Jeon
118ec9a8f2SSungbin Jeonclass Commonmark {
128ec9a8f2SSungbin Jeon    public static function RendtoDW($markdown): string {
138ec9a8f2SSungbin Jeon        # create environment
148ec9a8f2SSungbin Jeon        $environment = self::createDWEnvironment();
158ec9a8f2SSungbin Jeon
168ec9a8f2SSungbin Jeon        # create parser
178ec9a8f2SSungbin Jeon        $parser = new DocParser($environment);
188ec9a8f2SSungbin Jeon        # create Dokuwiki Renderer
198ec9a8f2SSungbin Jeon        $DWRenderer = new DWRenderer($environment);
208ec9a8f2SSungbin Jeon
218ec9a8f2SSungbin Jeon        $document = $parser->parse($markdown);
228ec9a8f2SSungbin Jeon        return $DWRenderer->renderBlock($document);
238ec9a8f2SSungbin Jeon    }
248ec9a8f2SSungbin Jeon
258ec9a8f2SSungbin Jeon    public static function createDWEnvironment(): Environment {
268ec9a8f2SSungbin Jeon        $environment = new Environment();
278ec9a8f2SSungbin Jeon        $environment->addExtension(new CommonMarkToDokuWikiExtension());
28f46768a8SSungbin Jeon        $environment->addExtension(new FootnoteToDokuwikiExtension());
29*78c882e7SSungbin Jeon        $environment->addExtension(new StrikethroughExtension());
30f46768a8SSungbin Jeon
31f46768a8SSungbin Jeon        $environment->mergeConfig([
32f46768a8SSungbin Jeon            'html_input' => 'strip',
33f46768a8SSungbin Jeon        ]);
348ec9a8f2SSungbin Jeon
358ec9a8f2SSungbin Jeon        return $environment;
368ec9a8f2SSungbin Jeon    }
378ec9a8f2SSungbin Jeon}
388ec9a8f2SSungbin Jeon
398ec9a8f2SSungbin Jeon?>