所以这有效:
create ["archive.html"] $ do
route idRoute
compile $ do
posts <- (myRecentFirst gitTimes) =<< loadAll "posts/**"
let archiveCtx =
listField "posts" (postCtx allTags allCategories gitTimes) (return posts) `mappend`
constField "title" "Archives" `mappend`
(postCtx allTags allCategories gitTimes)
makeItem ""
>>= loadAndApplyTemplate "templates/archive.html" archiveCtx
>>= loadAndApplyTemplate "templates/default.html" archiveCtx
>>= relativizeUrls
在 archive.html 中创建最近帖子的列表;这是沼泽标准,它来自我认为的教程之一。除了 postCtx,它有点复杂,但在这里不应该相关。
但是,我想在普通帖子的侧边栏中列出一些最近的帖子。问题是最近的帖子最终取决于他们自己。我尝试将帖子本身从它自己生成的列表中排除,但我找不到这样做的好地方。这是我到目前为止所得到的:
match "posts/**" $ do
route $ (gsubRoute "posts/" (const "")) `composeRoutes` setExtension "html"
compile $ do
recents <- (myRecentFirst gitTimes) =<< loadAll "posts/**"
let postsContext = postCtx allTags allCategories gitTimes `mappend`
-- Show recent posts
recentsNotSelfField "recents" (postCtx allTags allCategories gitTimes) (return $ take 3 recents)
pandocCompilerWithTransform hblogPandocReaderOptions hblogPandocWriterOptions (titleFixer titles)
>>= loadAndApplyTemplate "templates/post.html" postsContext
>>= loadAndApplyTemplate "templates/default.html" postsContext
>>= relativizeUrls
recentsNotSelfField :: String -> Context a -> Compiler [Item a] -> Context b
recentsNotSelfField key context items = Context $ \k _ i ->
if k == key then do
let myId = itemIdentifier i
strippedItems <- items
let remains = filter (\x -> (itemIdentifier x) /= myId) strippedItems
return $ ListField context remains
else
CA.empty
recentsNotSelfField应该生成一个包含除自身之外的所有最近的字段,但它似乎没有工作,或者它是错误的地方,因为:
Initialising...
Creating store...
Creating provider...
Running rules...
Checking for out-of-date items
Compiling
[ERROR] Hakyll.Core.Runtime.chase: Dependency cycle detected: posts/computing/contact.md depends on posts/computing/contact.md
到目前为止我被困住了。
编辑:
我看到Hakyll 说“检测到依赖循环:...”,这是 loadPosts 做的,所以我尝试了这个:
match "posts/**" $ do
route $ (gsubRoute "posts/" (const "")) `composeRoutes` setExtension "html"
compile $ do
myId <- getUnderlying
recents <- (myRecentFirst gitTimes) =<< loadAll ("posts/**" .&&. complement (fromList [myId]))
let postsContext = postCtx allTags allCategories gitTimes `mappend`
-- Show recent posts
listField "recents" (postCtx allTags allCategories gitTimes) (return $ take 3 recents)
pandocCompilerWithTransform hblogPandocReaderOptions hblogPandocWriterOptions (titleFixer titles)
>>= loadAndApplyTemplate "templates/post.html" postsContext
>>= loadAndApplyTemplate "templates/default.html" postsContext
>>= relativizeUrls
但这只是让我:
[ERROR] Hakyll.Core.Runtime.chase: Dependency cycle detected: posts/computing/contact.md depends on posts/computing/general/ched.md depends on posts/computing/contact.md
,换句话说,我最终得到了两个最近的循环。