git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: Fixed duplicated reference links

with `--reference-links` and `--reference-location=section`. Also ensure that there are no empty link references `[]`. Closes #3674.

commit7b3aaee15ab69cdf3125a214c2124b91622af759 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 17 May 2017, 16:23 (9 years ago) parent61e965b
2 files changed+41−11
src/Text/Pandoc/Writers/Markdown.hs+15−11 test/command/3674.md+26
src/Text/Pandoc/Writers/Markdown.hs+15−11
@@ -91,6 +91,7 @@ instance Default WriterEnv
9191
9292data WriterState = WriterState { stNotes :: Notes
9393 , stRefs :: Refs
94+ , stKeys :: Set.Set Key
9495 , stIds :: Set.Set String
9596 , stNoteNum :: Int
9697 }
@@ -98,6 +99,7 @@ data WriterState = WriterState { stNotes :: Notes
9899instance Default WriterState
99100 where def = WriterState{ stNotes = []
100101 , stRefs = []
102+ , stKeys = Set.empty
101103 , stIds = Set.empty
102104 , stNoteNum = 1
103105 }
@@ -798,19 +800,21 @@ getKey = toKey . render Nothing
798800-- Prefer label if possible; otherwise, generate a unique key.
799801getReference :: PandocMonad m => Attr -> Doc -> Target -> MD m Doc
800802getReference attr label target = do
801− st <- get
802− let keys = map (\(l,_,_) -> getKey l) (stRefs st)
803− case find (\(_,t,a) -> t == target && a == attr) (stRefs st) of
803+ refs <- gets stRefs
804+ case find (\(_,t,a) -> t == target && a == attr) refs of
804805 Just (ref, _, _) -> return ref
805806 Nothing -> do
806− label' <- case getKey label `elem` keys of
807− True -> -- label is used; generate numerical label
808− case find (\n -> Key n `notElem` keys) $
809− map show [1..(10000 :: Integer)] of
810− Just x -> return $ text x
811− Nothing -> throwError $ PandocSomeError "no unique label"
812− False -> return label
813− modify (\s -> s{ stRefs = (label', target, attr) : stRefs st })
807+ keys <- gets stKeys
808+ label' <- if isEmpty label || getKey label `Set.member` keys
809+ then case find (\n -> not (Key n `Set.member` keys)) $
810+ map show [1..(10000 :: Integer)] of
811+ Just x -> return $ text x
812+ Nothing ->
813+ throwError $ PandocSomeError "no unique label"
814+ else return label
815+ modify (\s -> s{ stRefs = (label', target, attr) : stRefs s,
816+ stKeys = Set.insert (getKey label') (stKeys s)
817+ })
814818 return label'
815819
816820-- | Convert list of Pandoc inline elements to markdown.
test/command/3674.mdnew file+26
@@ -0,0 +1,26 @@
1+Make sure we don't get duplicate reference links, even with
2+`--reference-location=section`.
3+
4+```
5+% pandoc --reference-links -t markdown --reference-location=section --atx-headers
6+# a
7+
8+![](a)
9+
10+# b
11+
12+![](b)
13+
14+^D
15+# a
16+
17+![][1]
18+
19+ [1]: a
20+
21+# b
22+
23+![][2]
24+
25+ [2]: b
26+```