From 8c8d3bacb82d942815a61d72bbd22ed9a984c9fb Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Thu, 23 Jul 2020 18:26:29 -0700
Subject: [PATCH] Markdown writer: use numerical labels for refs...

...that are longer than 999 characters or contain
square brackets. For conformity with commonmark.

Closes #6560
---
 src/Text/Pandoc/Writers/Markdown.hs | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index d0b50a8d6..e41273b27 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -47,6 +47,7 @@ import Text.Pandoc.Walk
 import Text.Pandoc.Writers.HTML (writeHtml5String)
 import Text.Pandoc.Writers.Math (texMathToInlines)
 import Text.Pandoc.XML (toHtml5Entities)
+import Data.Coerce (coerce)
 
 type Notes = [[Block]]
 type Ref   = (Text, Target, Attr)
@@ -903,6 +904,7 @@ getNextIndex = do
   prevRefs <- gets stPrevRefs
   refs <- gets stRefs
   i <- (+ 1) <$> gets stLastIdx
+  modify $ \s -> s{ stLastIdx = i }
   let refLbls = map (\(r,_,_) -> r) $ prevRefs ++ refs
   return $ findUsableIndex refLbls i
 
@@ -915,12 +917,15 @@ getReference attr label target = do
     Just (ref, _, _) -> return ref
     Nothing       -> do
       keys <- gets stKeys
-      case M.lookup (getKey label) keys of
+      let key = getKey label
+      let rawkey = coerce key
+      case M.lookup key keys of
            Nothing -> do -- no other refs with this label
-             (lab', idx) <- if isEmpty label
+             (lab', idx) <- if T.null rawkey ||
+                                 T.length rawkey > 999 ||
+                                 T.any (\c -> c == '[' || c == ']') rawkey
                                then do
                                  i <- getNextIndex
-                                 modify $ \s -> s{ stLastIdx = i }
                                  return (tshow i, i)
                                else
                                  return (render Nothing label, 0)
@@ -947,11 +952,10 @@ getReference attr label target = do
                     return lab'
                   Nothing -> do -- but this one is to a new target
                     i <- getNextIndex
-                    modify $ \s -> s{ stLastIdx = i }
                     let lab' = tshow i
                     modify (\s -> s{
                        stRefs = (lab', target, attr) : refs,
-                       stKeys = M.insert (getKey label)
+                       stKeys = M.insert key
                                    (M.insert (target, attr) i km)
                                          (stKeys s) })
                     return lab'