Skip to content

Commit 95a741c

Browse files
claude: Add Typst image alt text support for PDF/UA accessibility
Closes #13868 Workaround for Pandoc not passing alt text to Typst image() calls. When an image has alt text (from caption or explicit alt attribute), emit raw Typst with image(..., alt: "...") for accessibility. This is a temporary fix until jgm/pandoc#11394 is merged upstream. - Add alt text handling in render_typst_fixups() Image filter - Alt parameter placed second after filename (matches Pandoc PR) - Escape backslashes and quotes in alt text - Add comprehensive test coverage with unique test images - Replace test-image.png with smaller penrose.svg Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9c3c8f1 commit 95a741c

File tree

15 files changed

+230
-2
lines changed

15 files changed

+230
-2
lines changed

news/changelog-1.9.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ All changes included in 1.9:
4040
- ([#13589](https://github.com/quarto-dev/quarto-cli/issues/13589)): Fix callouts with invalid ID prefixes crashing with "attempt to index a nil value". Callouts with unknown reference types now render as non-crossreferenceable callouts with a warning, ignoring the invalid ID.
4141
- ([#13602](https://github.com/quarto-dev/quarto-cli/issues/13602)): Fix support for multiple files set in `bibliography` field in `biblio.typ` template partial.
4242
- ([#13775](https://github.com/quarto-dev/quarto-cli/issues/13775)): Fix brand fonts not being applied when using `citeproc: true` with Typst format. Format detection now properly handles Pandoc format variants like `typst-citations`.
43+
- ([#13868](https://github.com/quarto-dev/quarto-cli/issues/13868)): Add image alt text support for PDF/UA accessibility. Alt text from markdown captions and explicit `alt` attributes is now passed to Typst's `image()` function. (Temporary workaround until [jgm/pandoc#11394](https://github.com/jgm/pandoc/pull/11394) is merged.)
4344

4445
### `pdf`
4546

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,40 @@ function render_typst_fixups()
9494
if image.attributes["width"] ~= nil and type(width_as_number) == "number" then
9595
image.attributes["width"] = tostring(image.attributes["width"] / PANDOC_WRITER_OPTIONS.dpi) .. "in"
9696
end
97+
98+
-- Workaround for Pandoc not passing alt text to Typst image() calls
99+
-- See: https://github.com/jgm/pandoc/issues/XXXX (TODO: file upstream)
100+
local alt_text = image.attributes["alt"]
101+
if (alt_text == nil or alt_text == "") and #image.caption > 0 then
102+
alt_text = pandoc.utils.stringify(image.caption)
103+
end
104+
105+
if alt_text and #alt_text > 0 then
106+
-- Build image() parameters
107+
local params = {}
108+
109+
-- Source path (escape backslashes for Windows paths)
110+
local src = image.src:gsub('\\', '\\\\')
111+
table.insert(params, '"' .. src .. '"')
112+
113+
-- Alt text second (escape backslashes and quotes)
114+
local escaped_alt = alt_text:gsub('\\', '\\\\'):gsub('"', '\\"')
115+
table.insert(params, 'alt: "' .. escaped_alt .. '"')
116+
117+
-- Height if present
118+
if image.attributes["height"] then
119+
table.insert(params, 'height: ' .. image.attributes["height"])
120+
end
121+
122+
-- Width if present
123+
if image.attributes["width"] then
124+
table.insert(params, 'width: ' .. image.attributes["width"])
125+
end
126+
127+
-- Use #box() wrapper for inline compatibility
128+
return pandoc.RawInline("typst", "#box(image(" .. table.concat(params, ", ") .. "))")
129+
end
130+
97131
return image
98132
end,
99133
Div = function(div)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: "Alt text test"
3+
pdf-standard: ua-1
4+
keep-tex: true
5+
---
6+
7+
![This is the alt text](penrose.svg)
8+
Lines changed: 16 additions & 0 deletions
Loading
Lines changed: 16 additions & 0 deletions
Loading
Lines changed: 16 additions & 0 deletions
Loading
Lines changed: 16 additions & 0 deletions
Loading
Lines changed: 16 additions & 0 deletions
Loading
Lines changed: 16 additions & 0 deletions
Loading
Lines changed: 16 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)