Parsing markdown links

70 views
Skip to first unread message

fgene...@gmail.com

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

I would like to parse markdown links in such a way that `[link](linked_page.md)` 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
md_html
= intercalate ".html" . splitOn ".md"

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

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

How can I make it work properly?

Regards,
Fred

Jasper Van der Jeugt

unread,
Oct 29, 2019, 7:53:23 AM10/29/19
to hak...@googlegroups.com
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
`http://github.com/foo/bar/some-file.md`; we only want to change links
within our website.

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

Hope this helps!

Jasper
> --
> 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 hakyll+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/hakyll/77ab1343-5a3c-4d28-bd09-a6bcf30ab36c%40googlegroups.com.

fgene...@gmail.com

unread,
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!

Fred
> To unsubscribe from this group and stop receiving emails from it, send an email to hak...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages