我正在使用 Hakyll 制作一个博客索引页面,其中列出了我的所有帖子。每个帖子都有日期、标题、预览和标签列表字段。
<!-- /blog.html -->
<div class="list">
$for(posts)$
<div class="entry-date">$date$</div>
<div class="entry-taglist">
$for(tags)$
<div class="tag">$body$</div>
$endfor$
</div>
<div class="entry-title">$title$</div>
<div class="entry-preview">$preview$</div>
$endfor$
</div>
这些帖子的上下文如下:
---
title: S.P.Q.R.
tags: foo, bar1, bar2
---
如何填充上下文以满足上述模板?这是我在这篇文章之后的尝试:
main = do
...
match "blog.md" $ do
route $ setExtension "html"
compile $ do
posts <- recentFirst =<< loadAll "posts/*"
let indexCtx =
listField "posts" tagContext (return posts) `mappend`
defaultContext in
pandocCompiler
>>= loadAndApplyTemplate "templates/blogindex.html" indexCtx
>>= relativizeUrls
postCtx :: Context String
postCtx =
dateField "date" "%d/%m/%Y" `mappend`
defaultContext
listContextWith :: Context String -> String -> Context a
listContextWith ctx s = listField s ctx $ do
identifier <- getUnderlying
metadata <- getMetadata identifier
let metas = maybe [] (map trim . splitAll ",") $ lookupString s metadata
return $ map (\x -> Item (fromFilePath x) x) metas
listContext :: String -> Context a
listContext = listContextWith postCtx
tagContext = listContext "tags" <> defaultContext
该页面返回一个空的 div .entry-taglist
。