Created
February 12, 2019 23:08
-
-
Save ColeTownsend/d4ebe9d0142b20510abaeb4c3e8d4e73 to your computer and use it in GitHub Desktop.
Gatsby Image Lookup for Rich Text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { StaticQuery, graphql } from 'gatsby' | |
import Img from 'gatsby-image' | |
export default class RichTextImage extends React.Component { | |
render() { | |
return ( | |
<StaticQuery | |
query={graphql` | |
query { | |
allContentfulAsset( | |
filter: { | |
file: { contentType: { regex: "/^image/png|^image/jpeg/" } } | |
} | |
) { | |
edges { | |
node { | |
id | |
file { | |
url | |
fileName | |
contentType | |
} | |
title | |
fluid(quality: 95, maxWidth: 800, maxHeight: 600) { | |
base64 | |
tracedSVG | |
aspectRatio | |
src | |
srcSet | |
srcWebp | |
srcSetWebp | |
sizes | |
} | |
} | |
} | |
} | |
} | |
`} | |
render={data => { | |
return ( | |
<Img | |
fluid={ | |
data.allContentfulAsset.edges.find(element => { | |
// Match string after final slash | |
return ( | |
element.node.title === this.props.titleLookup['en-US'] | |
) | |
}).node.fluid | |
} | |
/> | |
) | |
}} | |
/> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment