summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-05-17 16:23:33 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2017-05-17 16:23:33 +0200
commit7b3aaee15ab69cdf3125a214c2124b91622af759 (patch)
treec182d28a85da49d9884657806258440fcddbbdf8
parent61e965b117bf8cc60d30f7f44582ed8b4bb2b9e1 (diff)
Markdown writer: Fixed duplicated reference links
with `--reference-links` and `--reference-location=section`. Also ensure that there are no empty link references `[]`. Closes #3674.
-rw-r--r--src/Text/Pandoc/Writers/Markdown.hs26
-rw-r--r--test/command/3674.md26
2 files changed, 41 insertions, 11 deletions
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index 37bb98f5f..b70716181 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -91,6 +91,7 @@ instance Default WriterEnv
data WriterState = WriterState { stNotes :: Notes
, stRefs :: Refs
+ , stKeys :: Set.Set Key
, stIds :: Set.Set String
, stNoteNum :: Int
}
@@ -98,6 +99,7 @@ data WriterState = WriterState { stNotes :: Notes
instance Default WriterState
where def = WriterState{ stNotes = []
, stRefs = []
+ , stKeys = Set.empty
, stIds = Set.empty
, stNoteNum = 1
}
@@ -798,19 +800,21 @@ getKey = toKey . render Nothing
-- Prefer label if possible; otherwise, generate a unique key.
getReference :: PandocMonad m => Attr -> Doc -> Target -> MD m Doc
getReference attr label target = do
- st <- get
- let keys = map (\(l,_,_) -> getKey l) (stRefs st)
- case find (\(_,t,a) -> t == target && a == attr) (stRefs st) of
+ refs <- gets stRefs
+ case find (\(_,t,a) -> t == target && a == attr) refs of
Just (ref, _, _) -> return ref
Nothing -> do
- label' <- case getKey label `elem` keys of
- True -> -- label is used; generate numerical label
- case find (\n -> Key n `notElem` keys) $
- map show [1..(10000 :: Integer)] of
- Just x -> return $ text x
- Nothing -> throwError $ PandocSomeError "no unique label"
- False -> return label
- modify (\s -> s{ stRefs = (label', target, attr) : stRefs st })
+ keys <- gets stKeys
+ label' <- if isEmpty label || getKey label `Set.member` keys
+ then case find (\n -> not (Key n `Set.member` keys)) $
+ map show [1..(10000 :: Integer)] of
+ Just x -> return $ text x
+ Nothing ->
+ throwError $ PandocSomeError "no unique label"
+ else return label
+ modify (\s -> s{ stRefs = (label', target, attr) : stRefs s,
+ stKeys = Set.insert (getKey label') (stKeys s)
+ })
return label'
-- | Convert list of Pandoc inline elements to markdown.
diff --git a/test/command/3674.md b/test/command/3674.md
new file mode 100644
index 000000000..09f4e0d1e
--- /dev/null
+++ b/test/command/3674.md
@@ -0,0 +1,26 @@
+Make sure we don't get duplicate reference links, even with
+`--reference-location=section`.
+
+```
+% pandoc --reference-links -t markdown --reference-location=section --atx-headers
+# a
+
+![](a)
+
+# b
+
+![](b)
+
+^D
+# a
+
+![][1]
+
+ [1]: a
+
+# b
+
+![][2]
+
+ [2]: b
+```