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

Adding Asset Component for Rich Text Renderer #32503

Merged
merged 8 commits into from
Jan 7, 2022
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
8 changes: 4 additions & 4 deletions examples/cms-contentful/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ After setting up the content model (either manually or by running `npm run setup

**Content model overview**

![Content model overview](./docs/content-model-overview.jpg)
![Content model overview](./docs/content-model-overview.png)

### Step 4. Populate Content

Expand All @@ -158,7 +158,7 @@ Next, create another entry with the content type **Post**:

**Important:** For each entry and asset, you need to click on **Publish**. If not, the entry will be in draft state.

![Published content entry](./docs/content-entry-publish.jpg)
![Published content entry](./docs/content-entry-publish.png)

### Step 5. Set up environment variables

Expand Down Expand Up @@ -212,15 +212,15 @@ http://localhost:3000/api/preview?secret=<CONTENTFUL_PREVIEW_SECRET>&slug={entry

Replace `<CONTENTFUL_PREVIEW_SECRET>` with its respective value in `.env.local`.

![Content preview setup](./docs/content-preview-setup.jpg)
![Content preview setup](./docs/content-preview-setup.png)

Once saved, go to one of the posts you've created and:

- **Update the title**. For example, you can add `[Draft]` in front of the title.
- The state of the post will switch to **CHANGED** automatically. **Do not** publish it. By doing this, the post will be in draft state.
- In the sidebar, you will see the **Open preview** button. Click on it!

![Content entry overview](./docs/content-entry-preview.jpg)
![Content entry overview](./docs/content-entry-preview.png)

You will now be able to see the updated title. To exit preview mode, you can click on **Click here to exit preview mode** at the top of the page.

Expand Down
18 changes: 17 additions & 1 deletion examples/cms-contentful/components/post-body.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { documentToReactComponents } from '@contentful/rich-text-react-renderer'
import { BLOCKS } from '@contentful/rich-text-types'
import markdownStyles from './markdown-styles.module.css'
import RichTextAsset from './rich-text-asset'

const customMarkdownOptions = (content) => ({
renderNode: {
[BLOCKS.EMBEDDED_ASSET]: (node) => (
<RichTextAsset
id={node.data.target.sys.id}
assets={content.links.assets.block}
/>
),
},
})

export default function PostBody({ content }) {
return (
<div className="max-w-2xl mx-auto">
<div className={markdownStyles['markdown']}>
{documentToReactComponents(content.json)}
{documentToReactComponents(
content.json,
customMarkdownOptions(content)
)}
</div>
</div>
)
Expand Down
11 changes: 11 additions & 0 deletions examples/cms-contentful/components/rich-text-asset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Image from 'next/image'

export default function RichTextAsset({ id, assets }) {
const asset = assets?.find((asset) => asset.sys.id === id)

if (asset?.url) {
return <Image src={asset.url} layout="fill" alt={asset.description} />
}

return null
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions examples/cms-contentful/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ author {
excerpt
content {
json
links {
assets {
block {
sys {
id
}
url
description
}
}
}
}
`

Expand Down
2 changes: 1 addition & 1 deletion examples/cms-contentful/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dependencies": {
"@contentful/rich-text-react-renderer": "^15.4.0",
"classnames": "2.3.1",
"date-fns": "2.22.1",
"date-fns": "2.28.0",
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
Expand Down