Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-remark-images): ensure query string is ignored when detecting images #11743

Merged
merged 6 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/gatsby-remark-images/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"is-relative-url": "^2.0.0",
"lodash": "^4.17.10",
"mdast-util-definitions": "^1.2.0",
"query-string": "^6.1.0",
"slash": "^1.0.0",
"unist-util-select": "^1.5.0",
"unist-util-visit-parents": "^2.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,41 @@ exports[`it transforms images in markdown 1`] = `
</span>
</a>"
`;

exports[`it transforms HTML img tags with query strings 1`] = `
"<a class=\\"gatsby-resp-image-link\\" href=\\"not-a-real-dir/image/my-image.jpeg\\" style=\\"display: block\\" target=\\"_blank\\" rel=\\"noopener\\">
<span class=\\"gatsby-resp-image-wrapper\\" style=\\"position: relative; display: block; max-width: 300px; margin-left: auto; margin-right: auto;\\">
<span class=\\"gatsby-resp-image-background-image\\" style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url(&apos;url(&apos;data:image/png;base64, iVBORw)&apos;); background-size: cover; display: block;\\"></span>
<img class=\\"gatsby-resp-image-image\\" style=\\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\\" alt=\\"my image\\" title=\\"\\" src=\\"not-a-real-dir/image/my-image.jpeg\\" srcset=\\"not-a-real-dir/image/my-image.jpeg, not-a-real-dir/image/my-image.jpeg\\" sizes=\\"(max-width: 650px) 100vw, 650px\\">
</span>
</a>"
`;

exports[`it transforms images in markdown with query strings 1`] = `
"<a
class=\\"gatsby-resp-image-link\\"
href=\\"not-a-real-dir/images/my-image.jpeg\\"
style=\\"display: block\\"
target=\\"_blank\\"
rel=\\"noopener\\"
>
<span
class=\\"gatsby-resp-image-wrapper\\"
style=\\"position: relative; display: block; max-width: 300px; margin-left: auto; margin-right: auto;\\"
>
<span
class=\\"gatsby-resp-image-background-image\\"
style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('url('data:image/png;base64, iVBORw)'); background-size: cover; display: block;\\"
></span>
<img
class=\\"gatsby-resp-image-image\\"
style=\\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\\"
alt=\\"image\\"
title=\\"\\"
src=\\"not-a-real-dir/images/my-image.jpeg\\"
srcset=\\"not-a-real-dir/images/my-image.jpeg, not-a-real-dir/images/my-image.jpeg\\"
sizes=\\"(max-width: 650px) 100vw, 650px\\"
/>
</span>
</a>"
`;
37 changes: 36 additions & 1 deletion packages/gatsby-remark-images/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.mock(`gatsby-plugin-sharp`, () => {
})

const Remark = require(`remark`)
const queryString = require(`query-string`)

const plugin = require(`../`)

Expand Down Expand Up @@ -53,7 +54,7 @@ const createPluginOptions = (content, imagePaths = `/`) => {
return {
files: [].concat(imagePaths).map(imagePath => {
return {
absolutePath: `${dirName}/${imagePath}`,
absolutePath: queryString.parseUrl(`${dirName}/${imagePath}`).url,
}
}),
markdownNode: createNode(content),
Expand Down Expand Up @@ -268,3 +269,37 @@ test(`it handles goofy nesting properly`, async () => {
expect(node.value).toMatchSnapshot()
expect(node.value).not.toMatch(`<html>`)
})

test(`it transforms HTML img tags with query strings`, async () => {
const imagePath = `image/my-image.jpeg?query=string`

const content = `
<img src="./${imagePath}">
`.trim()

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

expect(nodes.length).toBe(1)

const node = nodes.pop()
expect(node.type).toBe(`html`)
expect(node.value).toMatchSnapshot()
expect(node.value).not.toMatch(`<html>`)
})

test(`it transforms images in markdown with query strings`, async () => {
const imagePath = `images/my-image.jpeg?query=string`
const content = `

![image](./${imagePath})
`.trim()

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

expect(nodes.length).toBe(1)

const node = nodes.pop()
expect(node.type).toBe(`html`)
expect(node.value).toMatchSnapshot()
expect(node.value).not.toMatch(`<html>`)
})
21 changes: 17 additions & 4 deletions packages/gatsby-remark-images/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
const visitWithParents = require(`unist-util-visit-parents`)
const getDefinitions = require(`mdast-util-definitions`)
const path = require(`path`)
const queryString = require(`query-string`)
const isRelativeUrl = require(`is-relative-url`)
const _ = require(`lodash`)
const { fluid } = require(`gatsby-plugin-sharp`)
Expand Down Expand Up @@ -67,6 +68,18 @@ module.exports = (
}
)

const getImageInfo = uri => {
const { url, query } = queryString.parseUrl(uri)
return {
ext: path
.extname(url)
.split(`.`)
.pop(),
url,
query,
}
}

// Takes a node and generates the needed images and then returns
// the needed HTML replacement for the image
const generateImagesAndUpdateNode = async function(
Expand All @@ -80,7 +93,7 @@ module.exports = (
const parentNode = getNode(markdownNode.parent)
let imagePath
if (parentNode && parentNode.dir) {
imagePath = slash(path.join(parentNode.dir, node.url))
imagePath = slash(path.join(parentNode.dir, getImageInfo(node.url).url))
} else {
return null
}
Expand Down Expand Up @@ -113,7 +126,7 @@ module.exports = (
const presentationWidth = fluidResult.presentationWidth

// Generate default alt tag
const srcSplit = node.url.split(`/`)
const srcSplit = getImageInfo(node.url).url.split(`/`)
const fileName = srcSplit[srcSplit.length - 1]
const fileNameNoExt = fileName.replace(/\.[^/.]+$/, ``)
const defaultAlt = fileNameNoExt.replace(/[^A-Z0-9]/gi, ` `)
Expand Down Expand Up @@ -264,7 +277,7 @@ module.exports = (
return resolve()
}
}
const fileType = node.url.slice(-3)
const fileType = getImageInfo(node.url).ext

// Ignore gifs as we can't process them,
// svgs as they are already responsive by definition
Expand Down Expand Up @@ -328,7 +341,7 @@ module.exports = (
return resolve()
}

const fileType = formattedImgTag.url.slice(-3)
const fileType = getImageInfo(formattedImgTag.url).ext
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path.extname returns a file extension with a dot. In other words, given const file = 'asdfasdf.png'

  • path.extname(file) === '.png'
  • file.slice(-3) === 'png'

Wouldn't this then break the subsequent checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes, sorry!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should now be resolved by d10a826. Thanks so much for you guidance throughout this PR!


// Ignore gifs as we can't process them,
// svgs as they are already responsive by definition
Expand Down