You are here

protected function CommonMark::getEnvironment in Markdown 8.2

Retrieves a CommonMark environment, creating it if necessary.

Return value

\League\CommonMark\Environment The CommonMark environment.

1 call to CommonMark::getEnvironment()
CommonMark::converter in src/Plugin/Markdown/CommonMark/CommonMark.php
Retrieves a CommonMark converter instance.

File

src/Plugin/Markdown/CommonMark/CommonMark.php, line 384

Class

CommonMark
Support for CommonMark by The League of Extraordinary Packages.

Namespace

Drupal\markdown\Plugin\Markdown\CommonMark

Code

protected function getEnvironment() {
  if (!$this->environment) {
    $environment = $this
      ->createEnvironment();
    $settings = $this
      ->getSettings(TRUE);

    // Unless the render strategy is set to "none", force the following
    // settings so the parser doesn't attempt to filter things.
    if ($this
      ->getRenderStrategy() !== static::NONE) {
      $settings['allow_unsafe_links'] = TRUE;
      $settings['html_input'] = 'allow';
    }
    $extensions = $this
      ->extensions();
    foreach ($extensions as $extension) {

      /* @var \Drupal\markdown\Plugin\Markdown\CommonMark\ExtensionInterface $extension */

      // Skip disabled extensions.
      if (!$extension
        ->isEnabled()) {
        continue;
      }

      // Add extension settings.
      if ($extension instanceof SettingsInterface) {

        // Because CommonMark is highly extensible, any extension that
        // implements settings should provide a specific and unique settings
        // key to wrap its settings when passing it to the environment config.
        // In the off chance the extension absolutely must merge with the
        // root level, it can pass an empty value (i.e. '' or 0); NULL will
        // throw an exception and FALSE will ignore merging with the parsing
        // config altogether.
        $settingsKey = $extension
          ->settingsKey();
        if ($settingsKey === NULL || $settingsKey === TRUE) {
          throw new InvalidPluginDefinitionException($extension
            ->getPluginId(), sprintf('The "%s" markdown extension must also supply a value in %s. This is a requirement of the parser so it knows how extension settings should be merged.', $extension
            ->getPluginId(), '\\Drupal\\markdown\\Plugin\\Markdown\\SettingsInterface::settingsKey'));
        }

        // If the extension plugin specifies anything other than FALSE, merge.
        if ($settingsKey !== FALSE) {
          $extensionSettings = $extension
            ->getSettings(TRUE);
          if ($settingsKey) {
            $extensionSettings = [
              $settingsKey => $extensionSettings,
            ];
          }
          $settings = NestedArray::mergeDeep($settings, $extensionSettings);
        }
      }

      // Finally, let the extension register itself with the environment.
      // Note: this is our own custom method, that is used throughout the
      // commonmark based @ MarkdownExtension plugins so they can work
      // across multiple API versions where entire interfaces have changed.
      // @see \Drupal\markdown\Plugin\Markdown\CommonMark\ExtensionInterface::register()
      $extension
        ->register($environment);
    }

    // Merge settings into config.
    $environment
      ->setConfig(NestedArray::mergeDeep($environment
      ->getConfig(), $settings));
    $this->environment = $environment;
  }
  return $this->environment;
}