@@ -30,7 +30,7 @@ module Text.Pandoc.Chunks
30
30
import Text.Pandoc.Definition
31
31
import Text.Pandoc.Shared (makeSections , stringify , inlineListToIdentifier ,
32
32
tshow )
33
- import Text.Pandoc.Walk (Walkable (.. ))
33
+ import Text.Pandoc.Walk (Walkable (.. ), query )
34
34
import Data.Aeson (FromJSON , ToJSON )
35
35
import Data.Text (Text )
36
36
import Text.Printf (printf )
@@ -371,15 +371,20 @@ toTOCTree =
371
371
fixTOCTreePaths :: [Chunk ] -> Tree SecInfo -> Tree SecInfo
372
372
fixTOCTreePaths chunks = go " "
373
373
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)
375
379
mempty chunks
380
+ getIds :: Block -> [Text ]
381
+ getIds (Div (i," section" : _,_) _) = [i]
382
+ getIds _ = []
376
383
go :: FilePath -> Tree SecInfo -> Tree SecInfo
377
384
go fp (Node secinfo subtrees) =
378
385
let newpath = M. lookup (secId secinfo) idMap
379
386
fp' = fromMaybe fp newpath
380
- fragment = case newpath of
381
- Nothing -> " #" <> secId secinfo
382
- Just _ -> " " -- link to top of file
387
+ fragment = " #" <> secId secinfo
383
388
in Node secinfo{ secPath = T. pack fp' <> fragment }
384
389
(map (go fp') subtrees)
385
390
0 commit comments