@@ -20,6 +20,19 @@ local function convert_svg(path)
2020 end
2121end
2222
23+ local mimeImgExts = {
24+ [" image/jpeg" ]= " jpg" ,
25+ [" image/gif" ]= " gif" ,
26+ [" image/vnd.microsoft.icon" ]= " ico" ,
27+ [" image/avif" ]= " avif" ,
28+ [" image/bmp" ]= " bmp" ,
29+ [" image/png" ]= " png" ,
30+ [" image/svg+xml" ]= " svg" ,
31+ [" image/tiff" ]= " tif" ,
32+ [" image/webp" ]= " webp" ,
33+ }
34+
35+
2336-- A cache of image urls that we've resolved into the mediabag
2437-- keyed by {url: mediabagpath}
2538local resolvedUrls = {}
@@ -109,13 +122,31 @@ function pdfImages()
109122 else
110123 local relativePath = image .src :match (' https?://[%w%.%:]+/(.+)' )
111124 if relativePath then
112-
125+
113126 local imgMt , imgContents = pandoc .mediabag .fetch (image .src )
114- local decodedSrc = urldecode (image .src )
127+ local decodedSrc = fullyUrlDecode (image .src )
115128 if decodedSrc == nil then
116129 decodedSrc = " unknown"
117130 end
118- local filename = windows_safe_filename (tex_safe_filename (pandoc .path .filename (decodedSrc )))
131+
132+ local function filenameFromMimeType (filename , imgMt )
133+ -- Use the mime type to compute an extension when possible
134+ -- This will allow pandoc to properly know the type, even when
135+ -- the path to the image is a difficult to parse URI
136+ local mimeExt = mimeImgExts [imgMt ]
137+ if mimeExt then
138+ local stem , _ext = pandoc .path .split_extension (filename )
139+ return stem .. ' .' .. mimeExt
140+ else
141+ return filename
142+ end
143+ end
144+
145+ -- compute the filename for this file
146+ local basefilename = pandoc .path .filename (decodedSrc )
147+ local safefilename = windows_safe_filename (tex_safe_filename (basefilename ))
148+ local filename = filenameFromMimeType (safefilename , imgMt )
149+
119150 if imgMt ~= nil then
120151 local existingMt = pandoc .mediabag .lookup (filename )
121152 local counter = 1
@@ -139,3 +170,4 @@ function pdfImages()
139170 }
140171end
141172
173+
0 commit comments