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

refactor: align Rich Text field structure to Contentful GraphQL API #31122

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 67 additions & 50 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.fluid) {
return <GatsbyImage {...asset} style={{ width: 200 }} />
}
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 entries = data.allContentfulRichText.nodes
Expand All @@ -75,7 +75,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 @@ -93,77 +93,94 @@ export const pageQuery = graphql`
id
title
richText {
raw
references {
__typename
sys {
id
}
... on ContentfulAsset {
fluid(maxWidth: 200) {
...GatsbyContentfulFluid
}
}
... on ContentfulText {
title
short
}
... on ContentfulLocation {
location {
lat
lon
json
links {
assets {
block {
sys {
id
}
fluid(maxWidth: 200) {
...GatsbyContentfulFluid
}
}
}
... 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 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 @@ -8,6 +8,7 @@
},
"dependencies": {
"@babel/runtime": "^7.12.5",
"@contentful/rich-text-links": "^14.1.2",
"@contentful/rich-text-react-renderer": "^14.1.2",
"@contentful/rich-text-types": "^14.1.2",
"@hapi/joi": "^15.1.1",
Expand Down
Loading