View source
<?php
namespace Drupal\markdown\Plugin\Markdown\Parsedown;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Theme\ActiveTheme;
use Drupal\markdown\Plugin\Markdown\AllowedHtmlInterface;
use Drupal\markdown\Plugin\Markdown\BaseParser;
use Drupal\markdown\Plugin\Markdown\ParserInterface;
use Drupal\markdown\Plugin\Markdown\SettingsInterface;
use Drupal\markdown\Traits\ParserAllowedHtmlTrait;
use Drupal\markdown\Util\Composer;
use Drupal\markdown\Util\FilterHtml;
class Parsedown extends BaseParser implements AllowedHtmlInterface, SettingsInterface {
use ParserAllowedHtmlTrait {
allowedHtmlTags as allowedHtmlTagsTrait;
}
protected static $parsedownClass = '\\Parsedown';
protected static $version;
protected $parsedown;
public function __sleep() {
unset($this->parsedown);
return parent::__sleep();
}
public static function defaultSettings($pluginDefinition) {
return [
'breaks_enabled' => FALSE,
'markup_escaped' => FALSE,
'safe_mode' => FALSE,
'strict_mode' => FALSE,
'urls_linked' => TRUE,
] + parent::defaultSettings($pluginDefinition);
}
public function allowedHtmlTags(ParserInterface $parser, ActiveTheme $activeTheme = NULL) {
return FilterHtml::mergeAllowedTags($this
->allowedHtmlTagsTrait($parser, $activeTheme), [
'caption' => [],
'col' => [
'span' => TRUE,
],
'colgroup' => [
'span' => TRUE,
],
'del' => [],
'table' => [],
'tbody' => [],
'td' => [
'colspan' => TRUE,
'headers' => TRUE,
'rowspan' => TRUE,
],
'tfoot' => [],
'th' => [
'abbr' => TRUE,
'colspan' => TRUE,
'headers' => TRUE,
'rowspan' => TRUE,
'scope' => TRUE,
],
'thead' => [],
'tr' => [],
]);
}
public function buildConfigurationForm(array $element, FormStateInterface $form_state) {
$element = parent::buildConfigurationForm($element, $form_state);
$element += $this
->createSettingElement('breaks_enabled', [
'#access' => !!$this
->getSettingMethod('breaks_enabled'),
'#type' => 'checkbox',
'#title' => $this
->t('Automatic line breaks'),
'#description' => $this
->t('Enabling this will use line breaks (<code><br></code>) when a new line is detected instead of creating separate paragraphs (<code><p></code>).'),
], $form_state);
$element += $this
->createSettingElement('markup_escaped', [
'#access' => !!$this
->getSettingMethod('markup_escaped'),
'#type' => 'checkbox',
'#title' => $this
->t('Markup Escaped'),
'#description' => $this
->t('Enabling this will escape HTML markup.'),
], $form_state);
$this
->renderStrategyDisabledSettingState($form_state, $element['markup_escaped']);
$element += $this
->createSettingElement('safe_mode', [
'#access' => !!$this
->getSettingMethod('safe_mode'),
'#type' => 'checkbox',
'#title' => $this
->t('Safe Mode'),
'#description' => $this
->t('Enabling this will apply sanitization to additional scripting vectors (such as scripting link destinations) that are introduced by the markdown syntax itself.'),
], $form_state);
$this
->renderStrategyDisabledSettingState($form_state, $element['safe_mode']);
if ($this
->getRenderStrategy() !== static::NONE) {
$element['markup_escaped']['#value'] = FALSE;
$element['safe_mode']['#value'] = FALSE;
}
$element += $this
->createSettingElement('strict_mode', [
'#access' => !!$this
->getSettingMethod('strict_mode'),
'#type' => 'checkbox',
'#title' => $this
->t('Strict Mode'),
'#description' => $this
->t('Enables strict CommonMark compliance.'),
], $form_state);
$element += $this
->createSettingElement('urls_linked', [
'#access' => !!$this
->getSettingMethod('urls_linked'),
'#type' => 'checkbox',
'#title' => $this
->t('URLs linked'),
'#description' => $this
->t('Enabling this will automatically create links for URLs.'),
], $form_state);
return $element;
}
protected function convertToHtml($markdown, LanguageInterface $language = NULL) {
return $this
->getParsedown()
->text($markdown);
}
public function getParsedown() {
if (!$this->parsedown) {
$this->parsedown = new static::$parsedownClass();
$settings = $this
->getSettings();
if ($this
->getRenderStrategy() !== static::NONE) {
$settings['markup_escaped'] = FALSE;
$settings['safe_mode'] = FALSE;
}
foreach ($settings as $name => $value) {
if ($method = $this
->getSettingMethod($name)) {
$this->parsedown
->{$method}($value);
}
}
}
return $this->parsedown;
}
public function settingExists($name) {
return !!$this
->getSettingMethod($name);
}
protected function getSettingMethod($name) {
$map = static::settingMethodMap();
return isset($map[$name]) && method_exists(static::$parsedownClass, $map[$name]) ? $map[$name] : NULL;
}
protected static function settingMethodMap() {
return [
'breaks_enabled' => 'setBreaksEnabled',
'markup_escaped' => 'setMarkupEscaped',
'safe_mode' => 'setSafeMode',
'strict_mode' => 'setStrictMode',
'urls_linked' => 'setUrlsLinked',
];
}
}