From 0f27a403a5e445d75065f6d2bf1472fb615e570c Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Tue, 21 May 2019 16:48:59 +0200 Subject: [PATCH 1/5] Fixed an issue where captions would show filename in case of no caption --- packages/gatsby-remark-images/src/index.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/gatsby-remark-images/src/index.js b/packages/gatsby-remark-images/src/index.js index 302df3e059e0e..79ba966e43c28 100644 --- a/packages/gatsby-remark-images/src/index.js +++ b/packages/gatsby-remark-images/src/index.js @@ -81,10 +81,10 @@ module.exports = ( } } - const getNodeTitle = (node, alt) => { + const getNodeTitle = (node, alt, defaultAlt) => { if (node.title) { return node.title - } else if (alt && alt.length > 0) { + } else if (alt && alt.length > 0 && alt !== defaultAlt) { return alt } return `` @@ -250,7 +250,7 @@ module.exports = ( : options.wrapperStyle // Construct new image node w/ aspect ratio placeholder - const showCaptions = options.showCaptions && getNodeTitle(node, alt) + const showCaptions = options.showCaptions && getNodeTitle(node, alt, defaultAlt) let rawHTML = ` ${getNodeTitle( node, - alt + alt, + defaultAlt )} `.trim() From 96cae9cb78b4f25fd11cc77968b153280afd962e Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Tue, 21 May 2019 17:01:37 +0200 Subject: [PATCH 2/5] Fixed linting issues --- packages/gatsby-remark-images/src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-remark-images/src/index.js b/packages/gatsby-remark-images/src/index.js index 79ba966e43c28..cec2648efe108 100644 --- a/packages/gatsby-remark-images/src/index.js +++ b/packages/gatsby-remark-images/src/index.js @@ -250,7 +250,8 @@ module.exports = ( : options.wrapperStyle // Construct new image node w/ aspect ratio placeholder - const showCaptions = options.showCaptions && getNodeTitle(node, alt, defaultAlt) + const showCaptions = + options.showCaptions && getNodeTitle(node, alt, defaultAlt) let rawHTML = ` Date: Wed, 22 May 2019 00:49:32 +0200 Subject: [PATCH 3/5] Added test for no caption in case of no alt or title --- .../src/__tests__/__snapshots__/index.js.snap | 31 ++++++++++++++++++- .../src/__tests__/index.js | 15 +++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap b/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap index dbbbd2ff51ff5..f62994ae5248c 100644 --- a/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap +++ b/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap @@ -177,7 +177,7 @@ exports[`it uses tracedSVG placeholder when enabled 1`] = ` > " `; +exports[`showCaptions display nothing as caption if no title or alt 1`] = ` +" + + + \\"my + + " +`; + exports[`showCaptions display title as caption 1`] = ` "
{ expect($(`figcaption`).text()).toEqual(`some alt`) expect(node.value).toMatchSnapshot() }) + + it(`display nothing as caption if no title or alt`, async () => { + const imagePath = `images/my-image.jpeg` + const content = `![](./${imagePath})` + + const nodes = await plugin(createPluginOptions(content, imagePath), { + showCaptions: true, + }) + expect(nodes.length).toBe(1) + + const node = nodes.pop() + const $ = cheerio.load(node.value) + expect($(`figcaption`).length).toBe(0) + expect(node.value).toMatchSnapshot() + }) }) From 7445f3ba68db07840c02a0167340ed226cfd6667 Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Wed, 22 May 2019 01:11:57 +0200 Subject: [PATCH 4/5] Reverted weird change to snapshot --- .../src/__tests__/__snapshots__/index.js.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap b/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap index f62994ae5248c..e7b605fb96791 100644 --- a/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap +++ b/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap @@ -177,7 +177,7 @@ exports[`it uses tracedSVG placeholder when enabled 1`] = ` > Date: Wed, 22 May 2019 23:02:54 +0200 Subject: [PATCH 5/5] Removed snapshot test --- .../src/__tests__/__snapshots__/index.js.snap | 29 ------------------- .../src/__tests__/index.js | 1 - 2 files changed, 30 deletions(-) diff --git a/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap b/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap index e7b605fb96791..dbbbd2ff51ff5 100644 --- a/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap +++ b/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap @@ -224,35 +224,6 @@ exports[`showCaptions display alt as caption if title was not found 1`] = `
" `; -exports[`showCaptions display nothing as caption if no title or alt 1`] = ` -" - - - \\"my - - " -`; - exports[`showCaptions display title as caption 1`] = ` "
{ const node = nodes.pop() const $ = cheerio.load(node.value) expect($(`figcaption`).length).toBe(0) - expect(node.value).toMatchSnapshot() }) })