quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.91k stars 322 forks source link

Caption not showing when using inline SVG in Figure AST node #5297

Open mcanouil opened 1 year ago

mcanouil commented 1 year ago

This is to track possible enhancement/fix when inline HTML such as SVG is included in a Figure Pandoc AST node (such as the one emitted by diagram.lua below).

Note that the issue was raised in the discussion below when using a version of https://github.com/pandoc-ext/diagram under active development. A workaround has been found and is documented in the discussion.

Discussed in https://github.com/quarto-dev/quarto-cli/discussions/5266

Originally posted by **netw0rkf10w** April 21, 2023 Hello, I'm using [this filter](https://github.com/pandoc-ext/diagram/blob/main/diagram.lua) for converting TikZ code to SVG in Quarto. When using the filter as is, it works (an example can be found at https://raw.githubusercontent.com/pandoc-ext/diagram/main/test/input-tikz.md). However, when I changed the code to replace the SVG file with inline SVG, the caption is not showing (I checked the output HTML and the `figure` container was not even generated). More precisely, in the original code, an SVG image is created and store in `img`, then: ```lua -- Use the block's filename attribute or create a new name by hashing the image content. local basename, _extension = pandoc.path.split_extension( props.filename or pandoc.sha1(img) ) local fname = basename .. '.' .. extension_for_mimetype[imgtype] -- Store the data in the media bag: pandoc.mediabag.insert(fname, imgtype, img) -- Create a figure that contains just this image. local img_obj = pandoc.Image(props.alt, fname, "", props['image-attr']) return pandoc.Figure(pandoc.Plain{img_obj}, props.caption, props['fig-attr']) ``` What I did is, instead of creating a file in the media bag, I used directly the SVG as an inline HTML element: ```lua return pandoc.Figure(pandoc.RawInline('html', img), props.caption, props['fig-attr']) ``` (the above line replaces the entire previous code block) **Using Pandoc alone, it worked:** ``` quarto pandoc --lua-filter filters/diagram.lua -s tikz.qmd -o tikz.html ``` However, when rendering `tikz.qmd` containing ``` filters: - diagram.lua ``` with Quarto, the SVG is displayed but not in a figure container and thus there's no caption. Could you please help? Thank you very much in advance!
cscheid commented 1 year ago

I think the fundamental problem here is that a user filter is emitting a Figure, and our figure normalization pass happens before user filters.

Maybe what we should do is to expose the figure normalization pass as a quarto API, so that filters which emit Figure nodes can make sure that the output they generate is compatible with Quarto.