Skip to content

Commit

Permalink
fix(gatsby-remark-images): adding missing plugin options (#27944 and #…
Browse files Browse the repository at this point in the history
…27998) (#28035)

* fix(gatsby-remark-images): adding missing plugin options (#27944)

(cherry picked from commit 08447bd)

* fix(gatsby-remark-images): allow showCaptions to accept array of valid strings (#27998)

(cherry picked from commit 3675467)
  • Loading branch information
vladar authored Nov 13, 2020
1 parent 135e554 commit 537bc8f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
48 changes: 45 additions & 3 deletions packages/gatsby-remark-images/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ describe(`pluginOptionsSchema`, () => {
const expectedErrors = [
`"maxWidth" must be a number`,
`"linkImagesToOriginal" must be a boolean`,
`"showCaptions" must be a boolean`,
`"showCaptions" must be one of [boolean, array]`,
`"markdownCaptions" must be a boolean`,
`"sizeByPixelDensity" must be a boolean`,
`"wrapperStyle" must be one of [object, string]`,
`"backgroundColor" must be a string`,
`"quality" must be a number`,
`"withWebp" must be a boolean`,
`"withWebp" must be one of [object, boolean]`,
`"tracedSVG" must be a boolean`,
`"loading" must be one of [lazy, eager, auto]`,
`"disableBgImageOnAlpha" must be a boolean`,
Expand All @@ -29,7 +29,7 @@ describe(`pluginOptionsSchema`, () => {
wrapperStyle: true,
backgroundColor: 123,
quality: `This should be a number`,
withWebp: `This should be a boolean`,
withWebp: `This should be a boolean or an object`,
tracedSVG: `This should be a boolean`,
loading: `This should be lazy, eager or auto`,
disableBgImageOnAlpha: `This should be a boolean`,
Expand Down Expand Up @@ -60,4 +60,46 @@ describe(`pluginOptionsSchema`, () => {

expect(isValid).toBe(true)
})

it(`should validate the withWebp prop`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
withWebp: { quality: 100 },
})

expect(isValid).toBe(true)
})

describe(`allow to use array of valid strings for "showCaptions"`, () => {
it(`["title", "alt"]`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
showCaptions: [`title`, `alt`],
})

expect(isValid).toBe(true)
})

it(`["title"]`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
showCaptions: [`title`],
})

expect(isValid).toBe(true)
})

it(`["alt"]`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
showCaptions: [`alt`],
})

expect(isValid).toBe(true)
})

it(`["not valid"] (should fail validation)`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
showCaptions: [`not valid`],
})

expect(isValid).toBe(false)
})
})
})
12 changes: 10 additions & 2 deletions packages/gatsby-remark-images/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ exports.pluginOptionsSchema = function ({ Joi }) {
.description(
`Add a link to each image to the original image. Sometimes people want to see a full-sized version of an image e.g. to see extra detail on a part of the image and this is a convenient and common pattern for enabling this. Set this option to false to disable this behavior.`
),
showCaptions: Joi.boolean()
showCaptions: Joi.alternatives()
.try(
Joi.boolean(),
Joi.array().items(
Joi.string().valid(`title`),
Joi.string().valid(`alt`)
)
)
.default(false)
.description(
`Add a caption to each image with the contents of the title attribute, when this is not empty. If the title attribute is empty but the alt attribute is not, it will be used instead. Set this option to true to enable this behavior. You can also pass an array instead to specify which value should be used for the caption — for example, passing ['alt', 'title'] would use the alt attribute first, and then the title. When this is set to true it is the same as passing ['title', 'alt']. If you just want to use the title (and omit captions for images that have alt attributes but no title), pass ['title'].`
Expand Down Expand Up @@ -39,7 +46,8 @@ exports.pluginOptionsSchema = function ({ Joi }) {
quality: Joi.number()
.default(50)
.description(`The quality level of the generated files.`),
withWebp: Joi.boolean()
withWebp: Joi.alternatives()
.try(Joi.object({ quality: Joi.number() }), Joi.boolean())
.default(false)
.description(
`Additionally generate WebP versions alongside your chosen file format. They are added as a srcset with the appropriate mimetype and will be loaded in browsers that support the format. Pass true for default support, or an object of options to specifically override those for the WebP files. For example, pass { quality: 80 } to have the WebP images be at quality level 80.`
Expand Down

0 comments on commit 537bc8f

Please sign in to comment.