Skip to content

Instantly share code, notes, and snippets.

@ColeTownsend
Created February 12, 2019 23:08
Show Gist options
  • Save ColeTownsend/d4ebe9d0142b20510abaeb4c3e8d4e73 to your computer and use it in GitHub Desktop.
Save ColeTownsend/d4ebe9d0142b20510abaeb4c3e8d4e73 to your computer and use it in GitHub Desktop.
Gatsby Image Lookup for Rich Text
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