Skip to content

Commit ef69842

Browse files
committed
Chunks: improve fixTOCTreePaths.
We weren't adding ids for section headings that don't head a chunk, but these headings are needed for a TOC.
1 parent c1f87fd commit ef69842

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/Text/Pandoc/Chunks.hs

+10-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Text.Pandoc.Chunks
3030
import Text.Pandoc.Definition
3131
import Text.Pandoc.Shared (makeSections, stringify, inlineListToIdentifier,
3232
tshow)
33-
import Text.Pandoc.Walk (Walkable(..))
33+
import Text.Pandoc.Walk (Walkable(..), query)
3434
import Data.Aeson (FromJSON, ToJSON)
3535
import Data.Text (Text)
3636
import Text.Printf (printf)
@@ -371,15 +371,20 @@ toTOCTree =
371371
fixTOCTreePaths :: [Chunk] -> Tree SecInfo -> Tree SecInfo
372372
fixTOCTreePaths chunks = go ""
373373
where
374-
idMap = foldr (\chunk -> M.insert (chunkId chunk) (chunkPath chunk))
374+
idMap = foldr (\chunk m ->
375+
let ids = filter (not . T.null)
376+
(chunkId chunk :
377+
query getIds (chunkContents chunk))
378+
in foldr (\i -> M.insert i (chunkPath chunk)) m ids)
375379
mempty chunks
380+
getIds :: Block -> [Text]
381+
getIds (Div (i,"section":_,_) _) = [i]
382+
getIds _ = []
376383
go :: FilePath -> Tree SecInfo -> Tree SecInfo
377384
go fp (Node secinfo subtrees) =
378385
let newpath = M.lookup (secId secinfo) idMap
379386
fp' = fromMaybe fp newpath
380-
fragment = case newpath of
381-
Nothing -> "#" <> secId secinfo
382-
Just _ -> "" -- link to top of file
387+
fragment = "#" <> secId secinfo
383388
in Node secinfo{ secPath = T.pack fp' <> fragment }
384389
(map (go fp') subtrees)
385390

0 commit comments

Comments
 (0)