Skip to content

Commit c37302a

Browse files
committed
feat: order faq entries according to order metadata field
1 parent eabbc53 commit c37302a

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

site.hs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
{-# Language ViewPatterns #-}
44

55
import Hakyll
6-
import Control.Monad (filterM)
76
import Data.List (sortOn)
7+
import Control.Monad (filterM)
8+
import Control.Monad.ListM (sortByM)
89

910
--------------------------------------------------------------------------------------------------------
1011
-- MAIN GENERATION -------------------------------------------------------------------------------------
@@ -112,11 +113,7 @@ main = hakyll $ do
112113
route idRoute
113114
compile $ do
114115
sponsors <- sponsorsCtx . sortOn itemIdentifier <$> loadAll "donations/sponsors/*.markdown"
115-
entries <- loadAll "faq/*.markdown"
116-
117-
let ctx =
118-
listField "faq_entries" defaultContext (return entries) <>
119-
defaultContext
116+
ctx <- faqCtx <$> loadAll "faq/*.markdown"
120117

121118
makeItem ""
122119
>>= loadAndApplyTemplate "templates/faq/list.html" ctx
@@ -187,6 +184,12 @@ newsWithCategoriesCtx categories =
187184
newsCtx :: Context String
188185
newsCtx = newsWithCategoriesCtx categories
189186

187+
-- faq -------------------------------------------------------------------------------------------------
188+
faqCtx :: [Item String] -> Context String
189+
faqCtx entries =
190+
listField "faq_entries" defaultContext (sortFromMetadataField "order" entries) <>
191+
defaultContext
192+
190193
--------------------------------------------------------------------------------------------------------
191194
-- UTILS -----------------------------------------------------------------------------------------------
192195
--------------------------------------------------------------------------------------------------------
@@ -197,3 +200,11 @@ ofMetadataField field value = filterM (\item -> do
197200
mbStatus <- getMetadataField (itemIdentifier item) field
198201
return $ Just value == mbStatus
199202
)
203+
204+
-- | sort list of item based on the given metadata field
205+
sortFromMetadataField :: String -> [Item String] -> Compiler [Item String]
206+
sortFromMetadataField field = sortByM (\a b -> do
207+
a' <- getMetadataField (itemIdentifier a) field
208+
b' <- getMetadataField (itemIdentifier b) field
209+
return $ compare a' b'
210+
)

0 commit comments

Comments
 (0)