Skip to content

Commit

Permalink
fix(gatsby-remark-images): allow showCaptions to accept array of vali…
Browse files Browse the repository at this point in the history
…d strings (#27998)
  • Loading branch information
pieh authored Nov 12, 2020
1 parent 08447bd commit 3675467
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
36 changes: 35 additions & 1 deletion packages/gatsby-remark-images/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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]`,
Expand Down Expand Up @@ -68,4 +68,38 @@ describe(`pluginOptionsSchema`, () => {

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)
})
})
})
9 changes: 8 additions & 1 deletion 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

0 comments on commit 3675467

Please sign in to comment.