You are here

public function BaseParser::parse in Markdown 8.2

Same name and namespace in other branches
  1. 3.0.x src/Plugin/Markdown/BaseParser.php \Drupal\markdown\Plugin\Markdown\BaseParser::parse()

Parses markdown into HTML.

Parameters

string $markdown: The markdown string to parse.

\Drupal\Core\Language\LanguageInterface $language: Optional. The language of the markdown to be parsed.

Return value

\Drupal\markdown\Render\ParsedMarkdownInterface A safe ParsedMarkdown object.

Overrides ParserInterface::parse

See also

\Drupal\markdown\Render\ParsedMarkdownInterface

File

src/Plugin/Markdown/BaseParser.php, line 222

Class

BaseParser
Base class form Markdown Parser instances.

Namespace

Drupal\markdown\Plugin\Markdown

Code

public function parse($markdown, LanguageInterface $language = NULL) {
  $moduleHandler = \Drupal::moduleHandler();
  $renderStrategy = $this
    ->getRenderStrategy();
  if ($renderStrategy === static::ESCAPE_INPUT) {
    $markdown = Html::escape($markdown);
  }
  elseif ($renderStrategy === static::STRIP_INPUT) {
    $markdown = strip_tags($markdown);
  }

  // Invoke hook_markdown_alter().
  $context = $this
    ->getContext([
    'language' => $language,
  ]);
  $moduleHandler
    ->alter('markdown', $markdown, $context);

  // Convert markdown to HTML.
  $html = $this
    ->convertToHtml($markdown, $language);

  // Invoke hook_markdown_html_alter().
  $context['markdown'] = $markdown;
  $moduleHandler
    ->alter('markdown_html', $html, $context);

  // Filter all HTML output.
  if ($renderStrategy === static::FILTER_OUTPUT) {
    $html = (string) FilterHtml::fromParser($this)
      ->process($html, $language ? $language
      ->getId() : NULL);
  }
  return ParsedMarkdown::create($markdown, $html, $language)
    ->addCacheableDependency($this);
}