我想在我的 Hakyll 站点的上下文中添加一个字段。如果元数据中存在某个键,那么我想转换相应的值并将其包含在上下文中。如果元数据中不存在密钥,则不应将任何内容添加到上下文中。
我写了这个函数,应该做我所描述的:
-- | 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
但是,如果元数据字段不存在,那么这仍会在上下文中插入一个字段,并将空字符串作为其值。例如,我在我的上下文中有这一行:
transformedMetadataField "meta_description" "meta_description" escapeHtml
我有这个模板:
$if(meta_description)$
<meta name="description" content="$meta_description$"/>
$endif$
在元数据中没有 no 的页面上meta_description
,会生成以下 HTML:
<meta name="description" content=""/>
而我想要的是根本不生产任何标签。
transformedMetadataField
我在我的职能中做错了什么?