summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2024-01-29 23:03:12 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2024-01-29 23:04:45 -0800
commitef698423125611f63f7fc5d663c6df8244c8fe90 (patch)
tree092e9a831eb519f8ad866c740e905fe31f28e7ca /src
parentc1f87fdb37c904cb473d9747fd4141d4709c07d6 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Chunks.hs15
1 files changed, 10 insertions, 5 deletions
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)