b***@gmail.com
2017-10-15 17:59:05 UTC
Hi all,
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? Or is field not
suitable for what Iâm trying to use it for?
Thank you!
(Iâve also posted this question to Stack Overflow
<https://stackoverflow.com/q/46750357/371228>.)
Benjamin
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? Or is field not
suitable for what Iâm trying to use it for?
Thank you!
(Iâve also posted this question to Stack Overflow
<https://stackoverflow.com/q/46750357/371228>.)
Benjamin
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.