Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

custom render contenful rich text rendering

Copyimport { documentToReactComponents } from '@contentful/rich-text-react-renderer';
import { BLOCKS } from '@contentful/rich-text-types';

// Create a bespoke renderOptions object to target BLOCKS.EMBEDDED_ENTRY (linked entries e.g. videoEmbed
// and BLOCKS.EMBEDDED_ASSET (linked assets e.g. images)

const renderOptions = {
  renderNode: {
    [BLOCKS.EMBEDDED_ENTRY]: (node, children) => {
      if (node.data.target.sys.contentType.sys.id === 'videoEmbed') {
        return (
          <iframe
            src={node.data.target.fields.embedUrl}
            height="100%"
            width="100%"
            frameBorder="0"
            scrolling="no"
            title={node.data.target.fields.title}
            allowFullScreen={true}
          />
        );
      }
    },

    [BLOCKS.EMBEDDED_ASSET]: (node, children) => {
      // render the EMBEDDED_ASSET as you need
      return (
        <img
          src={`https://${node.data.target.fields.file.url}`}
          height={node.data.target.fields.file.details.image.height}
          width={node.data.target.fields.file.details.image.width}
          alt={node.data.target.fields.description}
        />
      );
    },
  },
};

export default function RichTextResponse({ richTextResponse }) {
  return <>{documentToReactComponents(richTextResponse, renderOptions)}</>;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: contoh penggunaan promise 
Javascript :: sum the two first minimum numbers in an array 
Javascript :: changing CSS with JS, using a function - strips all CSS and re-adds classes passed by 2nd parameter - as an Array 
Javascript :: element vs node 
Javascript :: parse int stackoverflow 
Javascript :: string to number javascript & remove text 
Javascript :: get moment from another moment 
Javascript :: apps script convert a1notation to row column 
Javascript :: dynamic components 
Javascript :: create model Obejctid mongoose 
Javascript :: sentry reports too much recursion 
Javascript :: history go back js oneline 
Javascript :: freecodecamp Drop it 
Javascript :: javascript categories input object example 
Javascript :: useEffect not working array changes 
Javascript :: jquery element by name 
Javascript :: trim para remover excesso de espaço  
Javascript :: ecmascript make file for one function 
Javascript :: @Scheduled cron expresssion 
Javascript :: http://www.passportjs.org/packages/passport-jwt/ 
Javascript :: getters and setters in java script 
Javascript :: staticDir storybook svg and images not loading 
Javascript :: js get each pair of values from an array 
Javascript :: descomponer un numero js 
Javascript :: redux extension link 
Javascript :: .catch() in promise will aslo return a promise 
Javascript :: convert json results 
Javascript :: The setTimeout() method receives the second parameter in 
Javascript :: JSX expression with JS template literals 
Javascript :: class function constructor 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =