From 2155b5f9bc5a08fa1044f8a7ba75ce40af630fb7 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 4 Apr 2025 10:16:23 -0700 Subject: 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. --- src/Text/Pandoc/Writers/Markdown.hs | 9 +++-- test/command/10755.md | 71 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 test/command/10755.md diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index 60cd4b370..c2e7e7f5e 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -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 = diff --git a/test/command/10755.md b/test/command/10755.md new file mode 100644 index 000000000..5a603cdfc --- /dev/null +++ b/test/command/10755.md @@ -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 +
+An image. + +
+``` + +``` +% 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. +::: +:::: +``` -- cgit v1.2.3