View source
<?php
namespace Drupal\markdown\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;
use Drupal\markdown\Plugin\Markdown\MarkdownGuidelinesAlterInterface;
use Drupal\markdown\Plugin\Markdown\MarkdownParserInterface;
class MarkdownFilterTipsForm extends FormBase {
protected $filter;
protected $parser;
public function getFormId() {
return 'markdown_filter_tips';
}
public function buildForm(array $form, FormStateInterface $form_state, bool $long = FALSE, MarkdownParserInterface $parser = NULL) {
if (!$parser) {
return [];
}
$this->parser = $parser;
$this->filter = $parser
->getFilter();
if ($long) {
$guidelines = $parser
->getGuidelines();
if ($parser instanceof MarkdownGuidelinesAlterInterface) {
$parser
->alterGuidelines($guidelines);
}
$form = $this
->buildGuidelines($guidelines);
}
else {
$form = $parser
->getSummary();
}
$form['#type'] = 'container';
return $form;
}
protected function buildGuidelines(array $guides = []) {
$allowedTags = $this->parser
->getAllowedTags();
$build = $this->parser
->getSummary();
$build['help']['#markup'] = '<p>' . $this
->t('This site renders CommonMark Markdown. While learning all of the Markdown syntax may feel intimidating at first, learning how to use a very small number of the most basic Markdown syntax is very easy. The following sections will provide examples for commonly used Markdown syntax, what HTML output it generates and how it will display on the site.') . '</p>';
$build['tips'] = [
'#type' => 'vertical_tabs',
];
$header = [
Html::escape($this->filter
->getLabel()),
$this
->t('HTML Output') . ' / ' . $this
->t('Rendered'),
];
foreach ($guides as $guide_id => $guide) {
$build['guides'][$guide_id] = [
'#title' => $guide['title'],
'#type' => 'fieldset',
'#group' => 'tips',
];
foreach ($guide['items'] as $key => $item) {
if (!empty($item['title'])) {
$build['guides'][$guide_id][$key]['title'] = [
'#type' => 'html_tag',
'#theme' => [
'html_tag__markdown_tip_title',
'html_tag',
],
'#tag' => 'h4',
'#attributes' => [
'class' => [
'title',
],
],
'#value' => $item['title'],
];
}
if (!empty($item['description'])) {
if (!is_array($item['description'])) {
$item['description'] = [
$item['description'],
];
}
if (count($item['description']) === 1) {
$build['guides'][$guide_id][$key]['description'] = [
'#type' => 'html_tag',
'#theme' => [
'html_tag__markdown_tip_description',
'html_tag',
],
'#tag' => 'div',
'#attributes' => [
'class' => [
'description',
],
],
'#value' => $item['description'][0],
];
}
else {
$build['guides'][$guide_id][$key]['description'] = [
'#theme' => [
'item_list__markdown_tip_description',
'item_list',
],
'#attributes' => [
'class' => [
'description',
],
],
'#items' => $item['description'],
];
}
}
if (empty($item['tags'])) {
continue;
}
$item_tags = array_keys($item['tags']);
$item_tags_not_allowed = array_diff($item_tags, $allowedTags ?? $item_tags);
if (count($item_tags_not_allowed) === count($item_tags)) {
continue;
}
foreach ($item_tags_not_allowed as $tag) {
unset($item['tags'][$tag]);
unset($item['titles'][$tag]);
unset($item['descriptions'][$tag]);
}
$rows = [];
foreach ($item['tags'] as $tag => $examples) {
if (!is_array($examples)) {
$examples = [
$examples,
];
}
foreach ($examples as $markdown) {
$row = [];
$rendered = (string) $this->parser
->convertToHtml($markdown);
if (!isset($item['strip_p']) || !empty($item['strip_p'])) {
$rendered = preg_replace('/^<p>|<\\/p>\\n?$/', '', $rendered);
}
$row[] = [
'data' => Markup::create('<pre><code class="language-markdown">' . Html::escape($markdown) . '</code></pre>'),
'style' => 'padding-right: 2em; width: 50%',
];
$row[] = [
'data' => Markup::create('<pre><code class="language-html">' . Html::escape($rendered) . '</code></pre>' . $rendered),
'style' => 'width: 50%',
];
$rows[] = $row;
}
}
$build['guides'][$guide_id][$key]['tags'] = [
'#theme' => 'table__markdown_tips',
'#header' => $header,
'#rows' => $rows,
'#sticky' => FALSE,
];
}
}
foreach (Element::children($build['guides']) as $child) {
if (!Element::getVisibleChildren($build['guides'][$child])) {
unset($build['guides'][$child]);
}
}
$entities = [
'&' => $this
->t('Ampersand'),
'•' => $this
->t('Bullet'),
'¢' => $this
->t('Cent'),
'©' => $this
->t('Copyright sign'),
'†' => $this
->t('Dagger'),
'‡' => $this
->t('Dagger (double)'),
'—' => $this
->t('Dash (em)'),
'–' => $this
->t('Dash (en)'),
'€' => $this
->t('Euro sign'),
'…' => $this
->t('Horizontal ellipsis'),
'>' => $this
->t('Greater than'),
'<' => $this
->t('Less than'),
'·' => $this
->t('Middle dot'),
' ' => $this
->t('Non-breaking space'),
'¶' => $this
->t('Paragraph'),
'‰' => $this
->t('Per mille sign'),
'£' => $this
->t('Pound sterling sign (GBP)'),
'®' => $this
->t('Registered trademark'),
'"' => $this
->t('Quotation mark'),
'™' => $this
->t('Trademark'),
'¥' => $this
->t('Yen sign'),
];
$rows = [];
foreach ($entities as $entity => $description) {
$rows[] = [
$description,
[
'data' => Markup::create('<kbd>' . Html::escape($entity) . '</kbd>'),
],
[
'data' => Markup::create($entity),
],
];
}
$build['guides']['entities'] = [
'#title' => $this
->t('HTML Entities'),
'#type' => 'fieldset',
'#group' => 'tips',
'description' => [
'#markup' => '<p>' . $this
->t('Most unusual characters can be directly entered without any problems.') . '</p>' . '<p>' . $this
->t('If you do encounter problems, try using HTML character entities. A common example looks like &amp; for an ampersand & character. For a full list of entities see HTML\'s <a href="@html-entities">entities</a> page. Some of the available characters include:', [
'@html-entities' => 'http://www.w3.org/TR/html4/sgml/entities.html',
]) . '</p>',
],
'table' => [
'#theme' => 'table__markdown_tips',
'#header' => [
$this
->t('HTML Entity'),
$this
->t('HTML code'),
$this
->t('Rendered'),
],
'#rows' => $rows,
'#sticky' => FALSE,
],
];
return $build;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}