-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle images without an extension as 'image' MIME and '.image' exten…
…sion (#178) * Use a generic 'image' MIME type for inline images whose URL does not end in an image file extension * Also bundle with EPUBs images that don’t have a characteristic file extension as `image` MIME type and `.image` file extension. * Default MIME image type: docs & refactoring
- Loading branch information
Showing
6 changed files
with
88 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,40 @@ | ||
import mimetype from 'mimetype'; | ||
|
||
const IMAGE_MIMETYPES = new Set([ | ||
'image/avif', | ||
'image/bmp', | ||
'image/gif', | ||
'image/jpeg', | ||
'image/png', | ||
'image/svg+xml', | ||
'image/tiff', | ||
'image/webp' | ||
]); | ||
|
||
/* | ||
Add newer image formats to the MIME type database. | ||
*/ | ||
mimetype.set('.webp', 'image/webp'); | ||
mimetype.set('.avif', 'image/avif'); | ||
|
||
export default function lookup(filepath) { | ||
export function lookupMimetype(filepath) { | ||
return mimetype.lookup(filepath); | ||
} | ||
|
||
export function extForMimetype(type) { | ||
return Object.entries(mimetype.catalog).find(it => it[1] === type)?.[0]; | ||
} | ||
|
||
export function getMimetypeFromURL(src, doc) { | ||
let pathname = src; | ||
try { | ||
pathname = new URL(src, doc.baseURI).pathname; | ||
} catch (err) { | ||
// no-op, probably due to bad `doc.baseURI` | ||
} | ||
return lookupMimetype(pathname); | ||
} | ||
|
||
export function isImageURL(src, doc) { | ||
return IMAGE_MIMETYPES.has(getMimetypeFromURL(src, doc)); | ||
} |