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): don’t add links if image is already linked #6982

Merged
merged 3 commits into from
Aug 6, 2018

Conversation

jlengstorf
Copy link
Contributor

Thanks for the help, @pieh!

fixes #6980

@KyleAMathews
Copy link
Contributor

KyleAMathews commented Aug 3, 2018

Deploy preview for using-postcss-sass failed.

Built with commit b610710

https://app.netlify.com/sites/using-postcss-sass/deploys/5b65025067610c74d2ed4d3a

@gatsbybot
Copy link
Collaborator

gatsbybot commented Aug 3, 2018

Deploy preview for using-drupal ready!

Built with commit b610710

https://deploy-preview-6982--using-drupal.netlify.com

@gatsbybot
Copy link
Collaborator

gatsbybot commented Aug 3, 2018

Deploy preview for gatsbygram ready!

Built with commit b610710

https://deploy-preview-6982--gatsbygram.netlify.com

const inLink = ancestors.some(ancestor => ancestor.type === `link`)
if (!inLink) {
markdownImageNodes.push(node)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is broken. We still need to fix the relative path, which I'm pretty sure @pieh told me to do and I just... didn't.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to process images in both cases (when it's inside link and when it's not) - difference should be whether we wrap image in link here - https://github.com/jlengstorf/gatsby/blob/af209f9215d8cd9c5ec2f3fda1cbdae1ec40027d/packages/gatsby-remark-images/src/index.js#L175-L188

@jlengstorf jlengstorf changed the title fix(gatsby-remark-images): don’t add links if image is already linked [WIP] fix(gatsby-remark-images): don’t add links if image is already linked Aug 3, 2018
@@ -240,6 +248,11 @@ module.exports = (

let imageRefs = []
$(`img`).each(function() {
// Make sure we don’t override already-linked images
if ($(this).closest(`a`).length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

unfortunately this won't work because a won't be in same html ast node - html nodes will contain single html tags from my research so <a href="http://example.com"><img src="./some-image.jpg"/></a> will have html ast node for a and children of that will be separate html ast node for img. I think this will need to be handled similiarly as checking if markdown image is in markdown link.

@pieh
Copy link
Contributor

pieh commented Aug 3, 2018

Following trend of nonsense examples we had yesterday: ;)
Do we want to handle cases like:

<a href="https://example.org">![Mix #1](./images/gatsby.png)</a>

or

[<img alt="mix #2" src="./images/gatsby.png"/>](https://example.org)

with mix of markdown and html?

I would say this is not in scope for this PR - I think we just need to handle markdown image in markdown link here.

@jlengstorf
Copy link
Contributor Author

Alright — I think I cracked this. Added a check for both link and <a> in the ancestor tree, and added tests for goofy nesting and mixed markup/markdown. This fixes the problem in my example repo for combos like this:

[![Screenshot of video.](./images/gatsby.png)](https://example.org)

<a href="https://google.com">**![test](./images/gatsby.png)**</a>

[**<img src="./images/gatsby.png"/>**](https://gatsbyjs.org)

@jlengstorf jlengstorf changed the title [WIP] fix(gatsby-remark-images): don’t add links if image is already linked fix(gatsby-remark-images): don’t add links if image is already linked Aug 4, 2018
@jlengstorf jlengstorf self-assigned this Aug 4, 2018
Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

Nice!

@pieh pieh merged commit 0146fc6 into gatsbyjs:master Aug 6, 2018
porfirioribeiro pushed a commit to porfirioribeiro/gatsby that referenced this pull request Aug 22, 2018
…gatsbyjs#6982)

* fix(gatsby-remark-images): don’t add links if image is already linked

fixes gatsbyjs#6980
Co-authored-by: @pieh

* fix: handle weird nesting and mixed MD/HTML

* fix: remove bad check for parent links
@jlengstorf jlengstorf deleted the fix/6980-remark-image-links branch May 17, 2019 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[gatsby-remark-images] Autolinking images overrides other links
4 participants