Skip to content

Commit

Permalink
refactor: align Rich Text field structure to Contentful GraphQL API (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Jan 6, 2022
1 parent bddce76 commit c79336c
Show file tree
Hide file tree
Showing 6 changed files with 1,433 additions and 752 deletions.
122 changes: 69 additions & 53 deletions e2e-tests/contentful/src/pages/rich-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ function renderReferencedComponent(ref) {
return <Component {...ref} />
}

const options = {
const makeOptions = ({ assetBlockMap, entryBlockMap, entryInlineMap }) => ({
renderMark: {
[MARKS.BOLD]: text => <strong data-cy-strong>{text}</strong>,
},
renderNode: {
[BLOCKS.EMBEDDED_ASSET]: node => {
const asset = node.data.target
const asset = assetBlockMap.get(node?.data?.target?.sys.id)
if (asset.gatsbyImageData) {
return <GatsbyImage image={asset.gatsbyImageData} />
}
Expand All @@ -40,7 +40,7 @@ const options = {
)
},
[BLOCKS.EMBEDDED_ENTRY]: node => {
const entry = node?.data?.target
const entry = entryBlockMap.get(node?.data?.target?.sys.id)
if (!entry) {
throw new Error(
`Entity not available for node:\n${JSON.stringify(node, null, 2)}`
Expand All @@ -49,7 +49,7 @@ const options = {
return renderReferencedComponent(entry)
},
[INLINES.EMBEDDED_ENTRY]: node => {
const entry = node.data.target
const entry = entryInlineMap.get(node?.data?.target?.sys.id)
if (entry.__typename === "ContentfulText") {
return (
<span data-cy-id="inline-text">
Expand All @@ -64,7 +64,7 @@ const options = {
)
},
},
}
})

const RichTextPage = ({ data }) => {
const defaultEntries = data.default.nodes
Expand All @@ -77,7 +77,7 @@ const RichTextPage = ({ data }) => {
return (
<div data-cy-id={slug} key={id}>
<h2>{title}</h2>
{renderRichText(richText, options)}
{renderRichText(richText, makeOptions)}
<hr />
</div>
)
Expand All @@ -89,7 +89,7 @@ const RichTextPage = ({ data }) => {
return (
<div data-cy-id={`english-${slug}`} key={id}>
<h2>{title}</h2>
{renderRichText(richTextLocalized, options)}
{renderRichText(richTextLocalized, makeOptions)}
<hr />
</div>
)
Expand All @@ -101,7 +101,7 @@ const RichTextPage = ({ data }) => {
return (
<div data-cy-id={`german-${slug}`} key={id}>
<h2>{title}</h2>
{renderRichText(richTextLocalized, options)}
{renderRichText(richTextLocalized, makeOptions)}
<hr />
</div>
)
Expand All @@ -125,76 +125,92 @@ export const pageQuery = graphql`
id
title
richText {
raw
references {
__typename
sys {
id
}
... on ContentfulAsset {
contentful_id
gatsbyImageData(width: 200)
}
... on ContentfulText {
title
short
}
... on ContentfulLocation {
location {
lat
lon
json
links {
assets {
block {
sys {
id
}
gatsbyImageData(width: 200)
}
}
... on ContentfulContentReference {
title
one {
entries {
block {
__typename
sys {
id
type
}
... on ContentfulText {
title
short
}
... on ContentfulLocation {
location {
lat
lon
}
}
... on ContentfulContentReference {
title
one {
__typename
sys {
id
}
... on ContentfulText {
title
short
}
... on ContentfulContentReference {
title
one {
... on ContentfulContentReference {
title
}
}
many {
... on ContentfulContentReference {
title
}
}
}
}
many {
__typename
sys {
id
}
... on ContentfulText {
title
short
}
... on ContentfulNumber {
title
integer
}
... on ContentfulContentReference {
title
one {
... on ContentfulContentReference {
title
}
}
many {
... on ContentfulContentReference {
title
}
}
}
}
}
}
many {
inline {
__typename
sys {
id
}
... on ContentfulText {
title
short
}
... on ContentfulNumber {
title
integer
}
... on ContentfulContentReference {
title
one {
... on ContentfulContentReference {
title
}
}
many {
... on ContentfulContentReference {
title
}
}
type
}
}
}
Expand All @@ -213,7 +229,7 @@ export const pageQuery = graphql`
id
title
richTextLocalized {
raw
json
}
}
}
Expand All @@ -228,7 +244,7 @@ export const pageQuery = graphql`
id
title
richTextLocalized {
raw
json
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-contentful/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dependencies": {
"@babel/runtime": "^7.15.4",
"@contentful/rich-text-react-renderer": "^14.1.3",
"@contentful/rich-text-links": "^14.1.2",
"@contentful/rich-text-types": "^14.1.2",
"@hapi/joi": "^15.1.1",
"@vercel/fetch-retry": "^5.0.3",
Expand Down
Loading

0 comments on commit c79336c

Please sign in to comment.