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 1 commit
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
13 changes: 9 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 @@ -80,7 +81,9 @@ 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, queryString.parseUrl(node.url).url)
)
} else {
return null
}
Expand Down Expand Up @@ -113,7 +116,7 @@ module.exports = (
const presentationWidth = fluidResult.presentationWidth

// Generate default alt tag
const srcSplit = node.url.split(`/`)
const srcSplit = queryString.parseUrl(node.url).url.split(`/`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not specific to your PR (this behavior already existed!), but this seems like a good opportunity for a helper, perhaps something like?

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

Here's an example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a helper in 185f8ca. The filename without the extension is pulled out for a default alt tag, which could be added to the helper. But that's the only place that's done, so I thought best to leave that part unless it's needed elsewhere. I'm happy to move it into the helper too if wanted though :-)

I'll look into adding a test next.

const fileName = srcSplit[srcSplit.length - 1]
const fileNameNoExt = fileName.replace(/\.[^/.]+$/, ``)
const defaultAlt = fileNameNoExt.replace(/[^A-Z0-9]/gi, ` `)
Expand Down Expand Up @@ -264,7 +267,7 @@ module.exports = (
return resolve()
}
}
const fileType = node.url.slice(-3)
const fileType = queryString.parseUrl(node.url).url.slice(-3)

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

const fileType = formattedImgTag.url.slice(-3)
const fileType = queryString
.parseUrl(formattedImgTag.url)
.url.slice(-3)

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