jaspervdj / hakyll

A static website compiler library in Haskell
jaspervdj.be/hakyll
Other
2.69k stars 411 forks source link

Example of YAML syntax support #1018

Open tomhepworth opened 7 months ago

tomhepworth commented 7 months ago

Hi, I am inexperienced with Haskell but using Hakyll as an excuse to try and improve at the language.

Is it possible to parse more complex YAML with nested elements, like this:

---
title: Homepage
values:
        - value1: 0 
        - value2: 5
---

and use them in templates? If so, could anyone provide an example of how to achieve this?

tomhepworth commented 7 months ago

So far I have

valuesCtx :: Context String  
valuesCtx = field "values" $ \item -> do 
    metadata <- getMetadata (itemIdentifier item)
    trace (show metadata) $ pure ()
    let nestedField = lookupString "values" metadata
    return $ fromMaybe "none" nestedField

Trace output: 
fromList [("author",String "me"),("published",String "2010-09-06"),("title",String "title"),("values",Array [Object (fromList [("value1",String "aaaaa")]),Object (fromList [("value2",String "bbbbbb")])])]

But obviously this will never work. Is there a way to extract the the array object from the metadata?

I can only see lookupString with the type signature starting String -> Metadata -> ...