Skip to content

Instantly share code, notes, and snippets.

@mcanouil
Last active November 20, 2025 22:03
Show Gist options
  • Select an option

  • Save mcanouil/7d3fc1efe7e700f61d70cba907f3c819 to your computer and use it in GitHub Desktop.

Select an option

Save mcanouil/7d3fc1efe7e700f61d70cba907f3c819 to your computer and use it in GitHub Desktop.
This Lua filter moves `width` and `height` attributes from a div of class "cell" to the image inside a nested div of class "cell-output-display", if present
--[[
# MIT License
#
# Copyright (c) 2025 Mickaël Canouil
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
]]
--[[
This Lua filter moves `width` and `height` attributes from a div of class "cell"
to the image inside a nested div of class "cell-output-display", if present.
The attributes are removed from the parent div after being transferred to the image.
The filter handles images that are either directly in the cell-output-display div
or wrapped in a Para element.
--]]
--- Move width/height attributes from cell div to image in cell-output-display.
---@param div table
---@return table|nil
function Div(div)
if not div.classes:includes("cell") then
return nil
end
local width = div.attributes["width"]
local height = div.attributes["height"]
if not (width or height) then
return nil
end
local modified = false
for i, el in ipairs(div.content) do
if el.t == "Div" and el.classes:includes("cell-output-display") then
div.content[i] = pandoc.walk_block(el, {
Image = function(img)
img.attributes["width"] = width
img.attributes["height"] = height
modified = true
return img
end
})
end
end
if modified then
div.attributes["width"] = nil
div.attributes["height"] = nil
end
return div
end
@mcanouil
Copy link
Author

mcanouil commented Nov 20, 2025

Usage

---
title: "Reproducible Quarto Document"
format: html
filters:
  - image-display-size.lua
---

```{python}
#| width: "50%"
import polars as pl
from plotnine import ggplot, aes, geom_point

# Create a simple DataFrame
df = pl.DataFrame({
    "x": [1, 2, 3, 4],
    "y": [2, 4, 6, 8]
})

# Plot using plotnine
(
    ggplot(df) +
    aes(x="x", y="y") +
    geom_point()
)
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment