Skip to content

Commit

Permalink
chore(gatsby-remark-images): Only convert supported image extensions (#…
Browse files Browse the repository at this point in the history
…32868)

* chore(gatsby-remark-images): Only convert supported image extensions

* chore: Note supportedExtensions source

* test: Validate videos aren't transformed by remark-images
  • Loading branch information
Mike-Dax committed Aug 24, 2021
1 parent c409352 commit 63eaabf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
11 changes: 11 additions & 0 deletions packages/gatsby-remark-images/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ test(`it leaves non-relative images alone`, async () => {
expect(result).toEqual([])
})

test(`it leaves files with unsupported file extensions alone`, async () => {
const imagePath = `video/my-video.mp4`
const content = `
![video](./${imagePath})
`.trim()

const result = await plugin(createPluginOptions(content, imagePath))

expect(result).toEqual([])
})

test(`it transforms images in markdown`, async () => {
const imagePath = `images/my-image.jpeg`
const content = `
Expand Down
25 changes: 14 additions & 11 deletions packages/gatsby-remark-images/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ const cheerio = require(`cheerio`)
const { slash } = require(`gatsby-core-utils`)
const chalk = require(`chalk`)

// Should be the same as https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-transformer-sharp/src/supported-extensions.js
const supportedExtensions = {
jpeg: true,
jpg: true,
png: true,
webp: true,
tif: true,
tiff: true,
}

// If the image is relative (not hosted elsewhere)
// 1. Find the image file
// 2. Find the image's size
Expand Down Expand Up @@ -421,13 +431,8 @@ module.exports = (
}
const fileType = getImageInfo(node.url).ext

// Ignore gifs as we can't process them,
// svgs as they are already responsive by definition
if (
isRelativeUrl(node.url) &&
fileType !== `gif` &&
fileType !== `svg`
) {
// Only attempt to convert supported extensions
if (isRelativeUrl(node.url) && supportedExtensions[fileType]) {
return generateImagesAndUpdateNode(
node,
resolve,
Expand Down Expand Up @@ -488,12 +493,10 @@ module.exports = (

const fileType = getImageInfo(formattedImgTag.url).ext

// Ignore gifs as we can't process them,
// svgs as they are already responsive by definition
// Only attempt to convert supported extensions
if (
isRelativeUrl(formattedImgTag.url) &&
fileType !== `gif` &&
fileType !== `svg`
supportedExtensions[fileType]
) {
const rawHTML = await generateImagesAndUpdateNode(
formattedImgTag,
Expand Down

0 comments on commit 63eaabf

Please sign in to comment.