From ef698423125611f63f7fc5d663c6df8244c8fe90 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Mon, 29 Jan 2024 23:03:12 -0800 Subject: 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. --- src/Text/Pandoc/Chunks.hs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Chunks.hs b/src/Text/Pandoc/Chunks.hs index 566b10f8d..1d85ac0b2 100644 --- a/src/Text/Pandoc/Chunks.hs +++ b/src/Text/Pandoc/Chunks.hs @@ -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) -- cgit v1.2.3