aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2025-09-15 10:31:46 +0200
committerJohn MacFarlane <[email protected]>2025-09-15 10:50:04 +0200
commitf7059959ed516bc719e5e8289df7a5ab89c858e3 (patch)
tree0c9067a4410905b2b2c32aa877dea0a09ac4517c
parent9c36d884d5713a43294fe0a1a7046d55d9ae57c3 (diff)
Markdown writer: improve handling of implicit figures.
Allow implicit figures when alt text differs from caption (in this case, we use an image attribute to add the alt). Closes #11140.
-rw-r--r--src/Text/Pandoc/Writers/Markdown.hs32
-rw-r--r--test/command/6925.md4
2 files changed, 19 insertions, 17 deletions
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index d70496ddf..9ff0e1d5b 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -720,30 +720,32 @@ blockToMarkdown' opts (DefinitionList items) = do
return $ mconcat contents <> blankline
blockToMarkdown' opts (Figure figattr capt body) = do
let combinedAttr imgattr = case imgattr of
- ("", cls, kv)
- | (figid, [], []) <- figattr -> Just (figid, cls, kv)
- _ -> Nothing
- let combinedAlt alt = case capt of
- Caption Nothing [] -> if null alt
- then Just [Str "image"]
- else Just alt
- Caption Nothing [Plain captInlines]
- | null alt || stringify captInlines == stringify alt
- -> Just captInlines
- Caption Nothing [Para captInlines]
- | null alt || stringify captInlines == stringify alt
- -> Just captInlines
+ ("", cls, kvs)
+ | (figid, [], []) <- figattr
+ -> Just (figid, cls, [(k,v) | (k,v) <- kvs
+ , k /= "alt" ||
+ v /= "" && v /= trim (stringify capt)])
_ -> Nothing
case body of
[Plain [Image imgAttr alt (src, ttl)]]
| isEnabled Ext_implicit_figures opts
- , Just descr <- combinedAlt alt
, Just imgAttr' <- combinedAttr imgAttr
, isEnabled Ext_link_attributes opts || imgAttr' == nullAttr
-> do
-- use implicit figures if possible
let tgt' = (src, fromMaybe ttl $ T.stripPrefix "fig:" ttl)
- contents <- inlineListToMarkdown opts [Image imgAttr' descr tgt']
+ let descr = case capt of
+ Caption _ bs -> blocksToInlines bs
+ -- add alt attribute if image description different from caption,
+ -- so this won't be lost:
+ let imgAttr'' = case imgAttr' of
+ (i,c,kv)
+ | not (null alt)
+ , Nothing <- lookup "alt" kv
+ , stringify descr /= stringify alt ->
+ (i, c, ("alt", stringify alt) : kv)
+ _ -> imgAttr'
+ contents <- inlineListToMarkdown opts [Image imgAttr'' descr tgt']
return $ contents <> blankline
_ ->
-- fallback to raw html if possible or div otherwise
diff --git a/test/command/6925.md b/test/command/6925.md
index e0d8e6870..3c55e2367 100644
--- a/test/command/6925.md
+++ b/test/command/6925.md
@@ -23,12 +23,12 @@ a
::: thm
**Theorem 1**. *a*
-![image](1.png)
+![](1.png)
:::
::: thm2
**Theorem 1**. a
-![image](1.png)
+![](1.png)
:::
```