vendor/league/commonmark/src/MarkdownConverter.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the league/commonmark package.
  5.  *
  6.  * (c) Colin O'Dell <colinodell@gmail.com>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace League\CommonMark;
  12. use League\CommonMark\Environment\EnvironmentInterface;
  13. use League\CommonMark\Output\RenderedContentInterface;
  14. use League\CommonMark\Parser\MarkdownParser;
  15. use League\CommonMark\Parser\MarkdownParserInterface;
  16. use League\CommonMark\Renderer\HtmlRenderer;
  17. use League\CommonMark\Renderer\MarkdownRendererInterface;
  18. class MarkdownConverter implements MarkdownConverterInterface
  19. {
  20.     /** @psalm-readonly */
  21.     protected EnvironmentInterface $environment;
  22.     /** @psalm-readonly */
  23.     protected MarkdownParserInterface $markdownParser;
  24.     /** @psalm-readonly */
  25.     protected MarkdownRendererInterface $htmlRenderer;
  26.     public function __construct(EnvironmentInterface $environment)
  27.     {
  28.         $this->environment $environment;
  29.         $this->markdownParser = new MarkdownParser($environment);
  30.         $this->htmlRenderer   = new HtmlRenderer($environment);
  31.     }
  32.     public function getEnvironment(): EnvironmentInterface
  33.     {
  34.         return $this->environment;
  35.     }
  36.     /**
  37.      * Converts Markdown to HTML.
  38.      *
  39.      * @param string $markdown The Markdown to convert
  40.      *
  41.      * @return RenderedContentInterface Rendered HTML
  42.      *
  43.      * @throws \RuntimeException
  44.      */
  45.     public function convertToHtml(string $markdown): RenderedContentInterface
  46.     {
  47.         $documentAST $this->markdownParser->parse($markdown);
  48.         return $this->htmlRenderer->renderDocument($documentAST);
  49.     }
  50.     /**
  51.      * Converts CommonMark to HTML.
  52.      *
  53.      * @see Converter::convertToHtml
  54.      *
  55.      * @throws \RuntimeException
  56.      */
  57.     public function __invoke(string $markdown): RenderedContentInterface
  58.     {
  59.         return $this->convertToHtml($markdown);
  60.     }
  61. }