Commit 39e8e5aa authored by Nicolas Lenz's avatar Nicolas Lenz ❄️
Browse files

Fix ordering in prev/next

parent baf03d5a
Loading
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ buildArticles navbar inputs = do
    templateArticle <- readFileUtf8 "template/article.html"
    templateIndex <- readFileUtf8 "template/index.html"

    articlesInput' <- read extraMeta `mapM` inputs
    articlesInput' <- sortArticlesPlus <$> read extraMeta `mapM` inputs
    let neighbors = getNeighbors $ snd <$> articlesInput'
    -- TODO Refactor!
    let articlesInput = map (\((prevM, nextM), (path, art)) -> (path, addMeta (Meta $ Map.fromList [("prev", fromMaybe (MetaString "") prevM), ("next", fromMaybe (MetaString "") nextM)]) art)) (zip neighbors articlesInput')
@@ -78,7 +78,8 @@ buildArticles navbar inputs = do

getNeighbors :: [Pandoc] -> [(Maybe MetaValue, Maybe MetaValue)]
getNeighbors articles = f <$> [0..(length articles - 1)] where
    f n = (getLink =<< articles `safeIndex` (n-1), getLink =<< articles `safeIndex` (n+1))
    -- Index + 1 is previous as the newest is first in the list
    f n = (getLink =<< articles `safeIndex` (n+1), getLink =<< articles `safeIndex` (n-1))

getLink :: Pandoc -> Maybe MetaValue
getLink (Pandoc meta _) = lookupMeta "link" meta
@@ -93,13 +94,20 @@ safeIndex (_:xs) n = safeIndex xs (n-1)
-- | Generate an index page from a list of Articles.
-- Expects each article to contain a meta value "link" with its link path.
generateIndex :: Meta -> [Pandoc] -> Pandoc
generateIndex extraMeta = generateIndex' . sort where
    sort = sortOn (Down . (\(Pandoc meta _) -> lookupMeta "date" meta))
generateIndex extraMeta = generateIndex' {- . sortArticles -} where  -- Sorting is done right at the beginning now
    generateIndex' articles = Pandoc metaOutput [] where
        metaOutput = extraMeta <> Meta (Map.fromList
            [("pagetitle", MetaString "Home"), ("articles", MetaList articlesOutput)])
        articlesOutput = generateIndexMeta <$> articles

-- TODO only plus is needed, refactor
-- | Sorts a list of pandocuments by their date meta values.
sortArticles :: [Pandoc] -> [Pandoc]
sortArticles = sortOn (Down . (\(Pandoc meta _) -> lookupMeta "date" meta))

sortArticlesPlus :: [(a, Pandoc)] -> [(a, Pandoc)]
sortArticlesPlus = sortOn (Down . (\(_, (Pandoc meta _)) -> lookupMeta "date" meta))

-- | Generates the Pandoc MetaMap for a document for the index page.
-- It contains the meta of the source, the link and the body.
generateIndexMeta :: Pandoc -> MetaValue