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.
This commit is contained in:
John MacFarlane
2024-01-29 23:04:45 -08:00
parent c1f87fdb37
commit ef69842312
+10 -5
View File
@@ -30,7 +30,7 @@ module Text.Pandoc.Chunks
import Text.Pandoc.Definition
import Text.Pandoc.Shared (makeSections, stringify, inlineListToIdentifier,
tshow)
import Text.Pandoc.Walk (Walkable(..))
import Text.Pandoc.Walk (Walkable(..), query)
import Data.Aeson (FromJSON, ToJSON)
import Data.Text (Text)
import Text.Printf (printf)
@@ -371,15 +371,20 @@ toTOCTree =
fixTOCTreePaths :: [Chunk] -> Tree SecInfo -> Tree SecInfo
fixTOCTreePaths chunks = go ""
where
idMap = foldr (\chunk -> M.insert (chunkId chunk) (chunkPath chunk))
idMap = foldr (\chunk m ->
let ids = filter (not . T.null)
(chunkId chunk :
query getIds (chunkContents chunk))
in foldr (\i -> M.insert i (chunkPath chunk)) m ids)
mempty chunks
getIds :: Block -> [Text]
getIds (Div (i,"section":_,_) _) = [i]
getIds _ = []
go :: FilePath -> Tree SecInfo -> Tree SecInfo
go fp (Node secinfo subtrees) =
let newpath = M.lookup (secId secinfo) idMap
fp' = fromMaybe fp newpath
fragment = case newpath of
Nothing -> "#" <> secId secinfo
Just _ -> "" -- link to top of file
fragment = "#" <> secId secinfo
in Node secinfo{ secPath = T.pack fp' <> fragment }
(map (go fp') subtrees)