3

I've created a README.md file in the custom module and would like to provide this as the help page in formatted HTML.

Raw text is being printed out, how do I get the HTML?

The module is drupal/markdown 3.0.0-rc1.

function cbi_bond_id_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
   case 'help.page.cbi_bond_id':
      $text = file_get_contents(dirname(__FILE__) . "/README.md");
      if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
        return '<pre>' . $text . '</pre>';
      }
      else {
        // Use the Markdown filter to render the README.
        $filter_manager = \Drupal::service('plugin.manager.filter');
        $settings = \Drupal::configFactory()
          ->get('markdown.settings')
          ->getRawData();
        $config = ['settings' => $settings];
        $filter = $filter_manager->createInstance('markdown', $config);
        $filterResult =  $filter->process($text, 'en');
        $text = $filterResult->getProcessedText();
          return '<div>' . $text->getHtml() . '</div>';
        }
    }
    return NULL;
}

1 Answer 1

5

I just tinkered a bit with your code and I believe that your parser settings are modifying your output. I was able to read in a markdown file and output it correctly, formatted as HTML, using the \Drupal\markdown\PluginManager\ParserManager service and setting the render strategy to none.

$text = file_get_contents(dirname(__FILE__) . "/README.md");
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
  return '<pre>' . $text . '</pre>';
}
else {
  /** @var \Drupal\markdown\PluginManager\ParserManagerInterface $parser_manager */
  $parser_manager = \Drupal::service('plugin.manager.markdown.parser');
  $parser = $parser_manager->getDefaultParser([
    'render_strategy' => ['type' => 'none'],
  ]);
  return $parser->parse($text);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.