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

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 ConverterInterfaceMarkdownConverterInterface
  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 $input The Markdown to convert
  40.      *
  41.      * @return RenderedContentInterface Rendered HTML
  42.      *
  43.      * @throws \RuntimeException
  44.      */
  45.     public function convert(string $input): RenderedContentInterface
  46.     {
  47.         $documentAST $this->markdownParser->parse($input);
  48.         return $this->htmlRenderer->renderDocument($documentAST);
  49.     }
  50.     /**
  51.      * Converts Markdown to HTML.
  52.      *
  53.      * @deprecated since 2.2; use {@link convert()} instead
  54.      *
  55.      * @param string $markdown The Markdown to convert
  56.      *
  57.      * @return RenderedContentInterface Rendered HTML
  58.      *
  59.      * @throws \RuntimeException
  60.      */
  61.     public function convertToHtml(string $markdown): RenderedContentInterface
  62.     {
  63.         \trigger_deprecation('league/commonmark''2.2.0''Calling "convertToHtml()" on a %s class is deprecated, use "convert()" instead.'self::class);
  64.         return $this->convert($markdown);
  65.     }
  66.     /**
  67.      * Converts CommonMark to HTML.
  68.      *
  69.      * @see MarkdownConverter::convert()
  70.      *
  71.      * @throws \RuntimeException
  72.      */
  73.     public function __invoke(string $markdown): RenderedContentInterface
  74.     {
  75.         return $this->convert($markdown);
  76.     }
  77. }