Code formatting not working with Pandoc in Haskell

When using the pandoc CLI with pandoc test.md -o test.html --highlight-style=pygments, code blocks in the input markdown file are converted to nicely formatted HTML as expected:

<div class="sourceCode" id="cb1"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> hello_world():</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>    <span class="bu">print</span>(<span class="st">&quot;Hello, world!&quot;</span>)</span></code></pre></div>

However, when trying to achieve the same results with the pandoc library in a Haskell program, using something like the following:

processMarkdown :: Text -> IO Text
processMarkdown filePath = runIOorExplode $ do
  let readerOpts = def {readerExtensions = enableExtension Ext_yaml_metadata_block (readerExtensions def)}
  result <- readMarkdown readerOpts filePath
  let writerOpts = def {writerHighlightStyle = Just pygments}
  writeHtml5String writerOpts result

the resulting code in the HTML is all outputted on one line:

<p><code>python def hello_world():     print("Hello, world!")</code></p>

How can I fix this?


  • I'm not sure, but I suspect that instead of the default set of extensions, you will need to use the extensions enabled for Pandoc-flavored Markdown. In other words, instead of (readerExtensions def), you should use pandocExtensions.