I’d like to add a field to my Hakyll site’s context. If a certain key is present in the metadata then I’d like to transform the corresponding value and include that in the context. If the key is not present in the metadata then nothing should be added to the context.
I wrote this function that should do what I described:
-- | Creates a new field based on the item's metadata. If the metadata field is
-- not present then no field will actually be created. Otherwise, the value will
-- be passed to the given function and the result of that function will be used
-- as the field's value.
transformedMetadataField :: String -> String -> (String -> String) -> Context a
transformedMetadataField key itemName f = field key $ \item -> do
fieldValue <- getMetadataField (itemIdentifier item) itemName
return $ maybe (fail $ "Value of " ++ itemName ++ " is missing") f fieldValue
However, if the metadata field is not present then this will still insert a field into the context with the empty string as its value. For example, I have this line in my context:
transformedMetadataField "meta_description" "meta_description" escapeHtml
and I have this template:
$if(meta_description)$
<meta name="description" content="$meta_description$"/>
$endif$
On pages with no meta_description
in their metadata, the following HTML is produced:
<meta name="description" content=""/>
whereas what I want is for no tag to be produced at all.
What have I done wrong in my transformedMetadataField
function?