Parsing markdown links

Skip to first unread message

Oct 29, 2019, 6:21:02 AM10/29/19
to hakyll

I would like to parse markdown links in such a way that `[link](` produce the correct html link `linked_page.html`.

Right now I'm able to change all ".md" into ".html" a bit too effectively and I'm looking for a solution to filter only links.

I'm new to Hakyll and Haskell, please excuse my stupid current code:

md_html :: String -> String
= intercalate ".html" . splitOn ".md"

:: Item String -> Compiler (Item String)
htmizeUrls item
= do
return $ (fmap $ md_html) item

    match "code/**" $ do
        route $ setExtension
        compile $ pandocCompiler
>>= loadAndApplyTemplate "templates/default.html" postCtx
>>= relativizeUrls
            >>= htmizeUrls

How can I make it work properly?


Jasper Van der Jeugt

Oct 29, 2019, 7:53:23 AM10/29/19
Hi Fred,

We'll abuse `splitExtension` from `System.FilePath` a bit -- it's really
supposed to work on file paths, but if we're just taking an extension it
also works on URLs.

import System.FilePath (splitExtension)

Hakyll provides a utility function `withUrls`, which allows you map a
`String -> String` function over the URLs in an HTML page.

htmlizeUrls :: Item String -> Compiler (Item String)
htmlizeUrls = return . fmap (withUrls htmlize)

Lastly, we want to use an extra guard with `isExternal` from Hakyll.
This prevents us from changing external links like
``; we only want to change links
within our website.

htmlize :: String -> String
htmlize url = case splitExtension url of
_ | isExternal url -> url
(name, ".md") -> name ++ ".html"
_ -> url

Hope this helps!

> --
> You received this message because you are subscribed to the Google Groups "hakyll" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> To view this discussion on the web visit

Oct 29, 2019, 9:44:31 PM10/29/19
to hakyll
Hi Jasper,

this is perfect. Thank you very much for you time and your work on this wonderful project!

> To unsubscribe from this group and stop receiving emails from it, send an email to
Reply all
Reply to author
0 new messages