You are here

function hook_markdown_html_alter in Markdown 8.2

Allows modules to alter the generated HTML after it has been parsed.

Note: this may introduce or cause performance issues when attempting to parse a lot of markdown content at the same time. If a parser is extensible, it's highly recommended that you create an extension for that specific parser rather than relying on this Drupal hook. This hook is primarily to assist in dealing with parsers that are not extensible and require manual manipulation of the parser itself.

Parameters

string $html: The HTML generated from the parser.

array $context: An associative array of context, containing:

  • parser: \Drupal\markdown\Plugin\Markdown\MarkdownParserInterface The parser that these HTML restrictions belong to.
  • filter: \Drupal\filter\Plugin\FilterInterface Optional. A filter that is associated with the parser, may not be set.
  • format: \Drupal\filter\Entity\FilterFormat Optional. A filter format entity that is associated with the filter, may not be set.
  • markdown: (string) The markdown that was used to generate the HTML.
  • language: (LanguageInterface) The language of the markdown, if known.
1 invocation of hook_markdown_html_alter()
BaseParser::parse in src/Plugin/Markdown/BaseParser.php
Parses markdown into HTML.

File

./markdown.api.php, line 86
Hooks and alters provided by the Markdown module.

Code

function hook_markdown_html_alter(&$html, array $context) {

  // Ignore non PHP Markdown Extra parsers.
  $parser = $context['parser'];
  if (!$parser instanceof \Drupal\markdown\Plugin\Markdown\PhpMarkdown\PhpMarkdownExtra) {
    return;
  }

  // Handle omitted footnotes by storing them somewhere. They can then be used
  // elsewhere, like in a custom block that appears in a sidebar region.
  $phpMarkdown = $parser
    ->getPhpMarkdown();
  if ($phpMarkdown->omit_footnotes && $phpMarkdown->footnotes_assembled) {

    // Create a hash based on the contents of the HTML output.
    // This can be used as the lookup identifier to load the footnotes later.
    $hash = \Drupal\Component\Utility\Crypt::hashBase64($html);
    \Drupal::keyValue('my_module.markdown.footnotes')
      ->set($hash, $phpMarkdown->footnotes_assembled);
  }
}