@@ -66,7 +66,7 @@ import Text.Pandoc.Writers.Math (texMathToInlines)
6666import Text.Pandoc.Writers.Shared
6767
6868type Notes = [[Block]]
69−type Ref = ([Inline], Target, Attr)
69+type Ref = (Doc, Target, Attr)
7070type Refs = [Ref]
7171
7272type MD m = ReaderT WriterEnv (StateT WriterState m)
@@ -235,8 +235,7 @@ keyToMarkdown :: PandocMonad m
235235 => WriterOptions
236236 -> Ref
237237 -> MD m Doc
238−keyToMarkdown opts (label, (src, tit), attr) = do
239− label' <- inlineListToMarkdown opts label
238+keyToMarkdown opts (label', (src, tit), attr) = do
240239 let tit' = if null tit
241240 then empty
242241 else space <> "\"" <> text tit <> "\""
@@ -792,22 +791,25 @@ blockListToMarkdown opts blocks = do
792791 else RawBlock "markdown" " "
793792 mapM (blockToMarkdown opts) (fixBlocks blocks) >>= return . cat
794793
794+getKey :: Doc -> Key
795+getKey = toKey . render Nothing
796+
795797-- | Get reference for target; if none exists, create unique one and return.
796798-- Prefer label if possible; otherwise, generate a unique key.
797−getReference :: PandocMonad m => Attr -> [Inline] -> Target -> MD m [Inline]
799+getReference :: PandocMonad m => Attr -> Doc -> Target -> MD m Doc
798800getReference attr label target = do
799801 st <- get
802+ let keys = map (\(l,_,_) -> getKey l) (stRefs st)
800803 case find (\(_,t,a) -> t == target && a == attr) (stRefs st) of
801804 Just (ref, _, _) -> return ref
802805 Nothing -> do
803− label' <- case find (\(l,_,_) -> l == label) (stRefs st) of
804− Just _ -> -- label is used; generate numerical label
805− case find (\n -> notElem [Str (show n)]
806− (map (\(l,_,_) -> l) (stRefs st)))
807− [1..(10000 :: Integer)] of
808− Just x -> return [Str (show x)]
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
809811 Nothing -> throwError $ PandocSomeError "no unique label"
810− Nothing -> return label
812+ False -> return label
811813 modify (\s -> s{ stRefs = (label', target, attr) : stRefs st })
812814 return label'
813815
@@ -1078,15 +1080,15 @@ inlineToMarkdown opts lnk@(Link attr txt (src, tit))
10781080 shortcutable <- asks envRefShortcutable
10791081 let useShortcutRefLinks = shortcutable &&
10801082 isEnabled Ext_shortcut_reference_links opts
1081− ref <- if useRefLinks then getReference attr txt (src, tit) else return []
1082− reftext <- inlineListToMarkdown opts ref
1083+ reftext <- if useRefLinks then getReference attr linktext (src, tit)
1084+ else return empty
10831085 return $ if useAuto
10841086 then if plain
10851087 then text srcSuffix
10861088 else "<" <> text srcSuffix <> ">"
10871089 else if useRefLinks
10881090 then let first = "[" <> linktext <> "]"
1089− second = if txt == ref
1091+ second = if getKey linktext == getKey reftext
10901092 then if useShortcutRefLinks
10911093 then ""
10921094 else "[]"