LeagueCommonMark.php in Markdown 3.0.x
File
src/Plugin/Markdown/LeagueCommonMark.php
View source
<?php
namespace Drupal\markdown\Plugin\Markdown;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Language\LanguageInterface;
use Drupal\markdown\Plugin\Markdown\Extension\CommonMarkRendererInterface;
use Drupal\markdown\Traits\MarkdownParserBenchmarkTrait;
use League\CommonMark\Block\Parser\BlockParserInterface;
use League\CommonMark\Block\Renderer\BlockRendererInterface;
use League\CommonMark\Environment;
use League\CommonMark\EnvironmentAwareInterface;
use League\CommonMark\Extension\ExtensionInterface;
use League\CommonMark\Inline\Parser\InlineParserInterface;
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
class LeagueCommonMark extends ExtensibleParser implements MarkdownParserBenchmarkInterface {
use MarkdownParserBenchmarkTrait;
protected static $converterClass = '\\League\\CommonMark\\CommonMarkConverter';
protected static $converters;
protected static $environments;
public static function installed() : bool {
return class_exists(static::$converterClass);
}
public static function version() : string {
if (static::installed()) {
$class = static::$converterClass;
return $class::VERSION;
}
}
public function convertToHtml($markdown, LanguageInterface $language = NULL) {
return $this
->getConverter()
->convertToHtml($markdown);
}
protected function getConverter() {
if (!isset(static::$converters[$this->filterId])) {
$environment = $this
->getEnvironment();
static::$converters[$this->filterId] = new static::$converterClass($this->settings, $environment);
}
return static::$converters[$this->filterId];
}
protected function createEnvironment() {
return Environment::createCommonMarkEnvironment();
}
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));
}
if ($extension instanceof EnvironmentAwareInterface && !$extension instanceof BlockParserInterface && !$extension instanceof InlineParserInterface) {
$extension
->setEnvironment($environment);
}
if ($extension instanceof ExtensionInterface) {
$environment
->addExtension($extension);
}
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);
}
}
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];
}
}