From 8fc57664f88c2f8cc006d19f662b1f40927b83ec Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Fri, 8 May 2020 07:56:39 -0700
Subject: [PATCH] Implement implicit_figures extension for commonmark reader.

Closes #6350.
---
 src/Text/Pandoc/Extensions.hs         | 1 +
 src/Text/Pandoc/Readers/CommonMark.hs | 7 ++++++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/Text/Pandoc/Extensions.hs b/src/Text/Pandoc/Extensions.hs
index 1db1a6545..e33a59e37 100644
--- a/src/Text/Pandoc/Extensions.hs
+++ b/src/Text/Pandoc/Extensions.hs
@@ -435,6 +435,7 @@ getAllExtensions f = universalExtensions <> getAll f
     extensionsFromList
     [ Ext_raw_html
     , Ext_raw_tex            -- only supported in writer (for math)
+    , Ext_implicit_figures
     , Ext_hard_line_breaks
     , Ext_smart
     ]
diff --git a/src/Text/Pandoc/Readers/CommonMark.hs b/src/Text/Pandoc/Readers/CommonMark.hs
index d1f732bf1..c63ea6392 100644
--- a/src/Text/Pandoc/Readers/CommonMark.hs
+++ b/src/Text/Pandoc/Readers/CommonMark.hs
@@ -76,7 +76,12 @@ addBlocks opts = foldr (addBlock opts) []
 
 addBlock :: ReaderOptions -> Node -> [Block] -> [Block]
 addBlock opts (Node _ PARAGRAPH nodes) =
-  (Para (addInlines opts nodes) :)
+  case addInlines opts nodes of
+    [Image attr alt (src,tit)]
+      | isEnabled Ext_implicit_figures opts
+         -- the "fig:" prefix indicates an implicit figure
+      -> (Para [Image attr alt (src, "fig:" <> tit)] :)
+    ils -> (Para ils :)
 addBlock _ (Node _ THEMATIC_BREAK _) =
   (HorizontalRule :)
 addBlock opts (Node _ BLOCK_QUOTE nodes) =