Skip to content

Commit cb352af

Browse files
authored
Merge pull request #9723 from quarto-dev/bugfix/9722
typst - support data URIs in images
2 parents 5ddabfd + 8f00622 commit cb352af

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

news/changelog-1.5.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ All changes included in 1.5:
7474
- ([#9293](https://github.com/quarto-dev/quarto-cli/pull/9293)): Add `toc-indent` to control indentation of entries in the table of contents.
7575
- ([#9671](https://github.com/quarto-dev/quarto-cli/issues/9671)): Reimplement `typst` subfloats to fix subfloat counters.
7676
- ([#9694](https://github.com/quarto-dev/quarto-cli/issues/9694)): Fix default callout (`::: callout ... ::: `) in Typst.
77+
- ([#9722](https://github.com/quarto-dev/quarto-cli/issues/9722)): Resolve data URI images in Typst.
7778
- Upgrade Typst to 0.11
7879
- Upgrade the Typst template to draw tables without grid lines by default, in accordance with latest Pandoc.
7980

src/resources/filters/modules/mediabag.lua

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,28 @@ local function fetch_and_store_image(src)
5555
return filename
5656
end
5757

58+
local function resolve_image_from_url(image)
59+
if resolved_url_cache[image.src] then
60+
image.src = resolved_url_cache[image.src]
61+
return image
62+
end
63+
local relativePath = image.src:match("https?://[%w%$%-%_%.%+%!%*%'%(%)%:%%]+/(.+)") or
64+
image.src:match("data:image/.+;base64,(.+)")
65+
if not (relativePath or param("has-resource-path", false)) then
66+
return nil
67+
end
68+
69+
local filename = fetch_and_store_image(image.src)
70+
if filename == nil then
71+
return nil
72+
end
73+
image.src = filename
74+
return image
75+
end
76+
5877
return {
5978
resolved_url_cache = resolved_url_cache,
6079
with_mediabag_contents = with_mediabag_contents,
61-
fetch_and_store_image = fetch_and_store_image
80+
fetch_and_store_image = fetch_and_store_image,
81+
resolve_image_from_url = resolve_image_from_url
6282
}

src/resources/filters/quarto-post/pdf-images.lua

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
-- svg.lua
22
-- Copyright (C) 2021 by RStudio, PBC
33

4-
local mediabag = require 'modules/mediabag'
5-
64
local function call_rsvg_convert(path)
75
local stem = pandoc.path.split_extension(path)
86
local output = stem .. '.pdf'
@@ -75,7 +73,7 @@ function pdfImages()
7573

7674
-- See if the path points to an SVG in the media bag
7775
-- (been generated by a filter, for example)
78-
return mediabag.with_mediabag_contents(image.src, function (mime_type, tempPath)
76+
return _quarto.modules.mediabag.with_mediabag_contents(image.src, function (mime_type, tempPath)
7977
if mime_type == 'image/svg+xml' then
8078
local convertedPath = call_rsvg_convert(tempPath)
8179
if convertedPath == nil then
@@ -97,23 +95,7 @@ function pdfImages()
9795
image.src = mbPath
9896
return image
9997
elseif mt == nil then
100-
if mediabag.resolved_url_cache[image.src] then
101-
image.src = mediabag.resolved_url_cache[image.src]
102-
return image
103-
end
104-
105-
local relativePath = image.src:match("https?://[%w%$%-%_%.%+%!%*%'%(%)%:%%]+/(.+)") or
106-
image.src:match("data:image/.+;base64,(.+)")
107-
if not (relativePath or param("has-resource-path", false)) then
108-
return nil
109-
end
110-
111-
local filename = mediabag.fetch_and_store_image(image.src)
112-
if filename == nil then
113-
return nil
114-
end
115-
image.src = filename
116-
return image
98+
return _quarto.modules.mediabag.resolve_image_from_url(image)
11799
end
118100
end)
119101
end

src/resources/filters/quarto-post/typst.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ function render_typst_fixups()
103103
return {
104104
traverse = "topdown",
105105
Image = function(image)
106+
image = _quarto.modules.mediabag.resolve_image_from_url(image) or image
107+
106108
-- if the width or height are "ratio" or "relative", in typst parlance,
107109
-- then we currently need to hide it from Pandoc 3.1.9 until
108110
-- https://github.com/jgm/pandoc/issues/9104 is properly fixed
@@ -121,6 +123,8 @@ function render_typst_fixups()
121123
local escaped_src = image.src:gsub("\\", "\\\\"):gsub("\"", "\\\"")
122124
return pandoc.RawInline("typst", "#box(" .. attr_str .. "image(\"" .. escaped_src .. "\"))")
123125
end
126+
127+
return image
124128
end,
125129
Div = function(div)
126130
local cod = quarto.utils.match(".cell/:child/.cell-output-display")(div)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
format: typst
3+
---
4+
5+
{{< placeholder >}}
6+
7+
Now with a caption:
8+
9+
![A caption]({{< placeholder >}}){#fig-1}
10+
11+
See @fig-1.

0 commit comments

Comments
 (0)