@@ -8,6 +8,7 @@ module Hakyll.Core.DirectedGraph
88 , neighbours
99 , reverse
1010 , reachableNodes
11+ , sanitize
1112 ) where
1213
1314import Prelude hiding (reverse)
@@ -59,9 +60,17 @@ reachableNodes set graph = reachable (setNeighbours set) set
5960 where
6061 reachable next visited
6162 | S.null next = visited
62− | otherwise = reachable (sanitize neighbours') (next `S.union` visited)
63+ | otherwise = reachable (sanitize' neighbours') (next `S.union` visited)
6364 where
64− sanitize = S.filter (`S.notMember` visited)
65− neighbours' = setNeighbours (sanitize next)
65+ sanitize' = S.filter (`S.notMember` visited)
66+ neighbours' = setNeighbours (sanitize' next)
6667
6768 setNeighbours = S.unions . map (flip neighbours graph) . S.toList
69+
70+-- | Remove all dangling pointers, i.e. references to notes that do
71+-- not actually exist in the graph.
72+--
73+sanitize :: Ord a => DirectedGraph a -> DirectedGraph a
74+sanitize (DirectedGraph graph) = DirectedGraph $ M.map sanitize' $ graph
75+ where
76+ sanitize' (Node t n) = Node t $ S.filter (`M.member` graph) n