From 807a574e9d33fcf66928388e342cc1436eb2346e Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Mon, 28 Mar 2022 18:50:03 -0700
Subject: [PATCH] JATS reader: strip 'ref-' from ref id in constructing CSL id.

This allows better round-tripping, because the JATS
writer adds the `ref-` prefix to the citation id to get
the ref element's id.
---
 src/Text/Pandoc/Readers/JATS.hs | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/Text/Pandoc/Readers/JATS.hs b/src/Text/Pandoc/Readers/JATS.hs
index 98cf5bdf5..2dee2bab3 100644
--- a/src/Text/Pandoc/Readers/JATS.hs
+++ b/src/Text/Pandoc/Readers/JATS.hs
@@ -460,7 +460,10 @@ parseRef e = do
                             x -> x) :) .
                   catMaybes <$> mapM (refElement c) (elChildren c)
       Nothing -> pure []   -- TODO handle mixed-citation
-  return $ Map.fromList (("id", toMetaValue $ attrValue "id" e) : refVariables)
+  -- allows round-tripping, since JATS writer puts ref- in front of citation ids:
+  let stripPref t = fromMaybe t $ T.stripPrefix "ref-" t
+  return $ Map.fromList (("id", toMetaValue $ stripPref $ attrValue "id" e)
+                        : refVariables)
 
 textContent :: Element -> Text
 textContent = strContent