From 914cf0b602f0585cfd1b401b956c8edfd129d47d Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Wed, 16 Dec 2020 15:37:40 -0800
Subject: [PATCH] Fix citeproc regression with duplicate references.

- Use dev version of citeproc, which handles duplicate
  ids better, preferring the last one in the list
  and discarding the rest.
- Ensure that inline citations take priority over external
  ones.

See jgm/citeproc#36.

This restores the behavior of pandoc-citeproc.
---
 cabal.project               | 8 ++++----
 pandoc.cabal                | 2 +-
 src/Text/Pandoc/Citeproc.hs | 3 ++-
 stack.yaml                  | 8 ++++----
 4 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/cabal.project b/cabal.project
index 091c78ce6..55d4abaa2 100644
--- a/cabal.project
+++ b/cabal.project
@@ -9,8 +9,8 @@ source-repository-package
   location: https://github.com/jgm/doctemplates.git
   tag: 7ccbf7df16edbc7c5d835d955b242c61fd4e6601
 
--- source-repository-package
---   type: git
---   location: https://github.com/jgm/citeproc
---   tag:  42b1d154b02435229acbe98ae0f17d01b757ee93
+source-repository-package
+  type: git
+  location: https://github.com/jgm/citeproc
+  tag: a8193fe375fa2354049bf9a967ba3bad4b1ba053
 
diff --git a/pandoc.cabal b/pandoc.cabal
index 9244d25cd..d21d5e03c 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -400,7 +400,7 @@ library
                  blaze-markup          >= 0.8      && < 0.9,
                  bytestring            >= 0.9      && < 0.12,
                  case-insensitive      >= 1.2      && < 1.3,
-                 citeproc              >= 0.2.0.1  && < 0.3,
+                 citeproc              >= 0.3      && < 0.4,
                  commonmark            >= 0.1.1.2  && < 0.2,
                  commonmark-extensions >= 0.2.0.4  && < 0.3,
                  commonmark-pandoc     >= 0.2      && < 0.3,
diff --git a/src/Text/Pandoc/Citeproc.hs b/src/Text/Pandoc/Citeproc.hs
index 770d571a6..ca1ab9f96 100644
--- a/src/Text/Pandoc/Citeproc.hs
+++ b/src/Text/Pandoc/Citeproc.hs
@@ -119,7 +119,8 @@ processCitations (Pandoc meta bs) = do
                         Nothing -> return []
                     Nothing -> return []
   let refs = map (linkifyVariables . legacyDateRanges)
-                 (inlineRefs ++ externalRefs)
+                 (externalRefs ++ inlineRefs)
+                 -- note that inlineRefs can override externalRefs
   let otherIdsMap = foldr (\ref m ->
                              case T.words . extractText <$>
                                   M.lookup "other-ids"
diff --git a/stack.yaml b/stack.yaml
index 518c24b63..92a80c4ff 100644
--- a/stack.yaml
+++ b/stack.yaml
@@ -20,10 +20,10 @@ extra-deps:
 - doctemplates:
   git: https://github.com/jgm/doctemplates.git
   commit: 7ccbf7df16edbc7c5d835d955b242c61fd4e6601
-- citeproc-0.2.0.1
-# - citeproc:
-#   git: https://github.com/jgm/citeproc.git
-#   commit: 42b1d154b02435229acbe98ae0f17d01b757ee93
+# - citeproc-0.2.0.1
+- citeproc:
+  git: https://github.com/jgm/citeproc.git
+  commit: a8193fe375fa2354049bf9a967ba3bad4b1ba053
 ghc-options:
    "$locals": -fhide-source-paths -Wno-missing-home-modules
 resolver: lts-16.23