@@ -119,7 +119,8 @@ writeEPUB opts doc@(Pandoc meta _) = do
119119 -- body pages
120120
121121 -- add level 1 header to beginning if none there
122− let blocks' = case blocks of
122+ let blocks' = addIdentifiers
123+ $ case blocks of
123124 (Header 1 _ _ : _) -> blocks
124125 _ -> Header 1 ("",[],[]) (docTitle meta) : blocks
125126
@@ -431,6 +432,18 @@ data IdentState = IdentState{
431432showChapter :: Int -> String
432433showChapter = printf "ch%03d.xhtml"
433434
435+-- Add identifiers to any headers without them.
436+addIdentifiers :: [Block] -> [Block]
437+addIdentifiers bs = evalState (mapM go bs) []
438+ where go (Header n (ident,classes,kvs) ils) = do
439+ ids <- get
440+ let ident' = if null ident
441+ then uniqueIdent ils ids
442+ else ident
443+ put $ ident' : ids
444+ return $ Header n (ident',classes,kvs) ils
445+ go x = return x
446+
434447-- Go through a block list and construct a table
435448-- correlating the automatically constructed references
436449-- that would be used in a normal pandoc document with
@@ -444,21 +457,19 @@ correlateRefs chapterHeaderLevel bs =
444457 , chapterIdents = []
445458 , identTable = [] }
446459 where go :: Block -> State IdentState ()
447− go (Header n _ ils) = do
460+ go (Header n (ident,_,_) ils) = do
448461 when (n <= chapterHeaderLevel) $
449462 modify $ \s -> s{ chapterNumber = chapterNumber s + 1
450463 , chapterIdents = [] }
451464 st <- get
452− let runningid = uniqueIdent ils (runningIdents st)
453465 let chapterid = showChapter (chapterNumber st) ++
454466 if n <= chapterHeaderLevel
455467 then ""
456468 else '#' : uniqueIdent ils (chapterIdents st)
457− modify $ \s -> s{ runningIdents = runningid : runningIdents st
469+ modify $ \s -> s{ runningIdents = ident : runningIdents st
458470 , chapterIdents = chapterid : chapterIdents st
459− , identTable = (runningid, chapterid) :
460− identTable st
461− }
471+ , identTable = (ident, chapterid) : identTable st
472+ }
462473 go _ = return ()
463474
464475-- Replace internal link references using the table produced