How do I convert Markdown to Html using Text.Pandoc (Haskell)?
Question
I have the following code:
import Text.Pandoc
myWriterOptions = defaultWriterOptions
{ writerHtml5 = True
, writerStrictMarkdown = False
}
markdownToHtml :: String -> Html
markdownToHtml = writeHtml myWriterOptions . readMarkdown defaultParserState
If I apply it to a markdown string:
"header\n=======\nA line of text.\n### A list ###\n- apple\n- orange\n- pear"
I end up with:
"<h1 id=\"header\">header</h1>\n<p>A line of text. ### A list ### - apple - orange - pear</p>"
Solution
Your code looks fine to me, and so does the output. I get the same output from Try Pandoc.
If you add newlines before the second heading and list, then it produces the output you're looking for. I wouldn't be surprised if other Markdown implementations require blank lines, too; after all, it would be pretty annoying if starting a line with a hyphen started a new list (think line wrapping).
OTHER TIPS
Pandoc API has changed a bit. This works for me:
{-# LANGUAGE OverloadedStrings #-}
module PandocDemo where
import Data.Monoid ((<>))
import Data.Profunctor (dimap)
import Data.Text (Text)
import qualified Data.Text as T
import Text.Pandoc
markdownToHtml :: Text -> Text
markdownToHtml = dimap inp out markdownToHtml'
where
inp = T.unpack
out = either (("Error: " <>) . tshow) T.pack
markdownToHtml' :: String -> Either PandocError String
markdownToHtml' s = writeHtmlString opts <$> readMarkdown def s
where
opts = def { writerHtml5 = True }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow