You are here

protected function LeagueCommonMark::getEnvironment in Markdown 3.0.x

Retrieves a CommonMark environment, creating it if necessary.

Return value

\League\CommonMark\Environment The CommonMark environment.

1 call to LeagueCommonMark::getEnvironment()
LeagueCommonMark::getConverter in src/Plugin/Markdown/LeagueCommonMark.php
Retrieves a CommonMark converter, creating it if necessary.

File

src/Plugin/Markdown/LeagueCommonMark.php, line 102

Class

LeagueCommonMark
Plugin annotation @MarkdownParser( id = "thephpleague/commonmark", label = @Translation("CommonMark"), url = "https://commonmark.thephpleague.com", )

Namespace

Drupal\markdown\Plugin\Markdown

Code

protected function getEnvironment() {
  if (!isset(static::$environments[$this->filterId])) {
    $environment = $this
      ->createEnvironment();
    $extensions = $this
      ->getExtensions(TRUE);
    foreach ($extensions as $extension) {
      if ($settings = $extension
        ->getSettings()) {
        $environment
          ->setConfig(NestedArray::mergeDeep($environment
          ->getConfig(), $settings));
      }

      // Allow standalone extensions to be aware of the environment.
      // This allows extensions to load external instances that may not be
      // able to be extended from base Drupal plugin class (which is needed
      // for discovery purposes).
      if ($extension instanceof EnvironmentAwareInterface && !$extension instanceof BlockParserInterface && !$extension instanceof InlineParserInterface) {
        $extension
          ->setEnvironment($environment);
      }
      if ($extension instanceof ExtensionInterface) {
        $environment
          ->addExtension($extension);
      }

      // Add Block extensions.
      if ($extension instanceof BlockParserInterface || $extension instanceof BlockRendererInterface && $extension instanceof CommonMarkRendererInterface) {
        if ($extension instanceof BlockParserInterface) {
          $environment
            ->addBlockParser($extension);
        }
        if ($extension instanceof BlockRendererInterface) {
          $environment
            ->addBlockRenderer($extension
            ->rendererClass(), $extension);
        }
      }

      // Add Inline extensions.
      if ($extension instanceof InlineParserInterface || $extension instanceof InlineRendererInterface && $extension instanceof CommonMarkRendererInterface) {
        if ($extension instanceof InlineParserInterface) {
          $environment
            ->addInlineParser($extension);
        }
        if ($extension instanceof InlineRendererInterface) {
          $environment
            ->addInlineRenderer($extension
            ->rendererClass(), $extension);
        }
      }
    }
    static::$environments[$this->filterId] = $environment;
  }
  return static::$environments[$this->filterId];
}