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

Detect active links

parent 528c031b
Loading
Loading
Loading
Loading
Loading
+19 −7
Original line number Diff line number Diff line
@@ -11,18 +11,30 @@ import Load
import Save

-- | Generate the Pandoc MetaValue for a navbar from a list of NavItem from a Nebelhorn config.
generateNavbarMeta :: [NavItem] -> MetaValue
generateNavbarMeta navItems = MetaList $ map f navItems where
generateNavbarMeta :: [NavItem] -> Maybe MetaValue -> MetaValue
generateNavbarMeta navItems (Just (MetaString link)) = MetaList $ map f navItems where
    f NavItem{..} = MetaMap $ Map.fromList
        [("name", MetaString $ unpack navItemName), ("link", MetaString $ unpack navItemLink)]
        [ ("name", MetaString $ unpack navItemName)
        , ("link", MetaString $ unpack navItemLink)
        , ("active", if unpack navItemLink == ("/" <> link) then MetaBool True else MetaBool False)
        ]
generateNavbarMeta navItems _ = MetaList $ map f navItems where
    f NavItem{..} = MetaMap $ Map.fromList
        [ ("name", MetaString $ unpack navItemName)
        , ("link", MetaString $ unpack navItemLink)
        , ("active", MetaBool False)
        ]

putNavbarPage :: [NavItem] -> Pandoc -> Pandoc
putNavbarPage navItems pandoc@(Pandoc meta _)
    = addMeta "navbar" (generateNavbarMeta navItems $ lookupMeta "link" meta) pandoc

-- Puts the navbar into all documents of a Nebelhorn.
putNavbar :: Nebelhorn -> Nebelhorn
putNavbar nebelhorn@Nebelhorn{..}
    = nebelhorn{nebelhornArticles = addMeta "navbar" navbar <$> nebelhornArticles,
    nebelhornPages = addMeta "navbar" navbar <$> nebelhornPages,
    nebelhornIndex = addMeta "navbar" navbar nebelhornIndex} where
        navbar = generateNavbarMeta nebelhornNavItems
    = nebelhorn{nebelhornArticles = putNavbarPage nebelhornNavItems <$> nebelhornArticles,
    nebelhornPages = putNavbarPage nebelhornNavItems <$> nebelhornPages,
    nebelhornIndex = putNavbarPage nebelhornNavItems nebelhornIndex}

-- | Gets the previous and next neigbors for each article in the list.
-- This only works if every pandocument has the link meta value set.