Markdown writer: render a figure with Para caption as implicit figure.

Also, when falling back to a Div with class `figure` for a figure
that can't be represented any other way, include a Div with class
`caption` containing the caption.

Closes #10755.
This commit is contained in:
John MacFarlane
2025-04-04 10:16:23 -07:00
parent 84946bf819
commit 2155b5f9bc
2 changed files with 78 additions and 2 deletions
+7 -2
View File
@@ -717,6 +717,8 @@ blockToMarkdown' opts (Figure figattr capt body) = do
else Just alt
Caption Nothing [Plain captInlines]
| captInlines == alt || null alt -> Just captInlines
Caption Nothing [Para captInlines]
| captInlines == alt || null alt -> Just captInlines
_ -> Nothing
case body of
[Plain [Image imgAttr alt (src, ttl)]]
@@ -763,8 +765,11 @@ figureToMarkdown opts attr@(ident, classes, kvs) capt body
writeHtml5String
opts{ writerTemplate = Nothing }
(Pandoc nullMeta [Figure attr capt body])
| otherwise = let attr' = (ident, ["figure"] `union` classes, kvs)
in blockToMarkdown' opts (Div attr' body)
| otherwise = do
let attr' = (ident, ["figure"] `union` classes, kvs)
let Caption _mbshort caption = capt
let captionBs = [Div ("",["caption"],[]) caption | not (null caption)]
blockToMarkdown' opts (Div attr' (body <> captionBs))
itemEndsWithTightList :: [Block] -> Bool
itemEndsWithTightList bs =
+71
View File
@@ -0,0 +1,71 @@
```
% pandoc -f native -t markdown
[ Figure
( "" , [] , [] )
(Caption
Nothing [ Para [ Str "An" , Space , Str "image." ] ])
[ Plain
[ Image
( ""
, []
, []
)
[ Str "An" , Space , Str "image." ]
( "media/rId20.jpg" , "" )
]
]
]
^D
![An image.](media/rId20.jpg)
```
```
% pandoc -f native -t markdown -t markdown-implicit_figures
[ Figure
( "" , [] , [] )
(Caption
Nothing [ Para [ Str "An" , Space , Str "image." ] ])
[ Plain
[ Image
( ""
, []
, []
)
[ Str "An" , Space , Str "image." ]
( "media/rId20.jpg" , "" )
]
]
]
^D
<figure>
<img src="media/rId20.jpg" alt="An image." />
<figcaption aria-hidden="true"><p>An image.</p></figcaption>
</figure>
```
```
% pandoc -f native -t markdown -t markdown-implicit_figures-raw_html
[ Figure
( "" , [] , [] )
(Caption
Nothing [ Para [ Str "An" , Space , Str "image." ] ])
[ Plain
[ Image
( ""
, []
, []
)
[ Str "An" , Space , Str "image." ]
( "media/rId20.jpg" , "" )
]
]
]
^D
:::: figure
![An image.](media/rId20.jpg)
::: caption
An image.
:::
::::
```