Score:3

How do I use the markdown module to print the module README.md file?

km flag

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;
}
Score:4
bd flag

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);
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.