diff options
author | John MacFarlane <jgm@berkeley.edu> | 2020-07-23 18:26:29 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2020-07-23 18:26:29 -0700 |
commit | 8c8d3bacb82d942815a61d72bbd22ed9a984c9fb (patch) | |
tree | 8119babbb1b901413fc5d87d14dcd27db80a6240 /src/Text | |
parent | 5a216f7bd75c3f89c6d6217a8676e6fca6229cf7 (diff) | |
download | pandoc-8c8d3bacb82d942815a61d72bbd22ed9a984c9fb.tar.gz |
Markdown writer: use numerical labels for refs...
...that are longer than 999 characters or contain
square brackets. For conformity with commonmark.
Closes #6560
Diffstat (limited to 'src/Text')
-rw-r--r-- | src/Text/Pandoc/Writers/Markdown.hs | 14 |
1 files 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' |