aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Heller <[email protected]>2025-01-21 20:54:47 -0500
committerGitHub <[email protected]>2025-01-21 17:54:47 -0800
commitb95645b0a0c26afb22e51d1e220e79a0c47a927c (patch)
tree05ebdd690024f8adf96092b0c173f8d80d3af2d4 /src
parentba04a9987dd8b2251f93231dbf9aa378fd7ac001 (diff)
Prefer MIME type when determining extensions for MediaBag items (#10557)
Currently, remote images added to the MediaBag are stored at paths with extensions determined based on the external URI. For instance, an image from https://example.com/image.png is stored as <hash>.png. If the URI does not contain an extension (e.g., https://example.com/image), then the content-type of the downloaded image is used to determine the extension. This change switches the precedence such that content-type is preferred over extensions contained in the URI. This is necessary because some images are located at URIs with misleading extensions -- shields.io, for instance, serves SVGs from URIs with .yml extensions. With this change, the image/svg+xml content-type is now preferred over the .yml URI extension. This fixes a bug in the PDF writer in which such an image would be mishandled due to not being identified as an SVG.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/MediaBag.hs8
1 files changed, 5 insertions, 3 deletions
diff --git a/src/Text/Pandoc/MediaBag.hs b/src/Text/Pandoc/MediaBag.hs
index 678399703..1a4ea2ba1 100644
--- a/src/Text/Pandoc/MediaBag.hs
+++ b/src/Text/Pandoc/MediaBag.hs
@@ -107,9 +107,11 @@ insertMedia fp mbMime contents (MediaBag mediamap)
_ -> getMimeTypeDef fp''
mt = fromMaybe fallback mbMime
path = maybe fp'' (unEscapeString . uriPath) uri
- ext = case takeExtension path of
- '.':e | '%' `notElem` e -> '.':e
- _ -> maybe "" (\x -> '.':T.unpack x) $ extensionFromMimeType mt
+ ext = case extensionFromMimeType mt of
+ Just e -> '.':T.unpack e
+ Nothing -> case takeExtension path of
+ '.':e | '%' `notElem` e -> '.':e
+ _ -> ""
-- | Lookup a media item in a 'MediaBag', returning mime type and contents.
lookupMedia :: FilePath