Search code examples
haskellmarkdownpandoc

Using github alerts with pandoc


There is an Ext_alert extension in pandoc which handles alerts like this:

↪ echo "> [!WARNING]" | pandoc -t html -f gfm
<div class="warning">
<div class="title">
<p>Warning</p>
</div>
</div>

I'm trying to use it inside my hakyll project manually:

myPandocCompiler :: Compiler (Item String)
myPandocCompiler =
    pandocCompilerWithTransformM ropt wopt (pygmentsHighlight . shiftHeaders 1)
    where
        ropt = defaultHakyllReaderOptions { readerExtensions = enableExtension Ext_alerts pandocExtensions }
        wopt = defaultHakyllWriterOptions { writerExtensions = enableExtension Ext_alerts pandocExtensions }

But it doesn't work.

I can bypass hakyll and use pandoc only with the same result:

λ> ropt = defaultHakyllReaderOptions { readerExtensions = getDefaultExtensions "gfm" }
λ> wopt = defaultHakyllWriterOptions { writerExtensions = getDefaultExtensions "gfm" }
λ> readerExtensions ropt
Extensions (fromList [Ext_alerts,Ext_auto_identifiers,Ext_autolink_bare_uris,Ext_emoji,Ext_footnotes,Ext_gfm_auto_identifiers,Ext_pipe_tables,Ext_raw_html,Ext_strikeout,Ext_task_lists,Ext_tex_math_dollars,Ext_tex_math_gfm,Ext_yaml_metadata_block])
λ>
λ>
λ> runPure $ writeHtml5String wopt =<< readMarkdown ropt  ("> [!WARNING]" :: Text)
Right "<blockquote>\n<p>[!WARNING]</p>\n</blockquote>"

What am I doing wrong? How to readMarkdown and writeHtml5String using pandoc, converting alerts?


Solution

  • From The Pandoc Changelog that introduced the alerts extension (emphasis mine):

    Add alerts markdown extension. This enables GitHub style markdown alerts as a commonmark extension. This extension is now default for gfm. It can't be used with markdown, only with commonmark and variants.

    Changing from readMarkdown to readCommonMark seems to work how you want:

    main = T.putStrLn =<< (runIOorExplode $ writeHtml5String wopt =<< readCommonMark ropt  ("> [!WARNING]" :: Text))
    
    <div class="warning">
    <div class="title">
    <p>Warning</p>
    </div>
    </div>
    

    Looking at the Hakyll source reveals that it uses readMarkdown under the hood, so you may want to call readCommonMark directly somewhere in your Hakyll program.