[Gatsby & Contentful] sourcing rich text from Contenful

2022. 12. 7.·🎨 프론트엔드 공부/기타

📍참고

https://www.gatsbyjs.com/blog/how-to-use-the-contentful-rich-text-field-with-gatsby/

 

How To Use The Contentful Rich Text Field with Gatsby | Gatsby

Contentful’s Rich Text Editor provides content creators with powerful text editing capabilities via the use of Contentful’s "What you see is what you get" (wysiwyg) editor.

www.gatsbyjs.com

https://github.com/contentful/rich-text/tree/master/packages/rich-text-plain-text-renderer

 

GitHub - contentful/rich-text: Libraries for handling and rendering Rich Text 📄

Libraries for handling and rendering Rich Text 📄. Contribute to contentful/rich-text development by creating an account on GitHub.

github.com

 

📍Contentful CMS 에서 rich text type 데이터를 Gatsby 에서 소싱하기

1️⃣GraphQL 에서 richtext 이름 { raw } 로 쿼리

- 예시

{
  allContentfulPosts {
    nodes {
      richText {
        raw
      }
    }
  }
}

 

- raw 는 "Bacon\",\"marks\":[{\"type\":\"bold\"}] 같은 JSON 형식이기 때문에 JSON.parse 메서드로 파싱을 해줘야 함

 

2️⃣ renderRichText 헬퍼 함수 이용

options를 설정하는 방법이 복잡하고 코드가 쓸데없이 길어짐

import { renderRichText } from 'gatsby-source-contentful/rich-text'

return <div>{renderRichText(richText.raw, options)}</div>

 

✅2️⃣Contentful 에서 제공하는 패키지 이용

- 설치

npm install @contentful/rich-text-plain-text-renderer

 

- 예시

import { documentToPlainTextString } from '@contentful/rich-text-plain-text-renderer';

const document = {
  nodeType: 'document',
  data: {},
  content: [
    {
      nodeType: 'paragraph',
      data: {},
      content: [
        {
          nodeType: 'text',
          value: 'Hello',
          marks: [{ type: 'bold' }],
          data: {},
        },
        {
          nodeType: 'text',
          value: ' world!',
          marks: [{ type: 'italic' }],
          data: {},
        },
      ],
    },
  ],
};

documentToPlainTextString(document); // -> Hello world!

 

- 메서드 사용 전에 richText.raw 를 JSON.parse 로 파싱해서 인수로 넣어야 함!

'🎨 프론트엔드 공부/기타' 카테고리의 다른 글
  • [원티드 FE 프리온보딩] 0. Intro
  • [Gatsby & GraphQL] gatsbyImageData 리졸버
  • Gatsby Image components - StaticImage vs GatsbyImage
  • ESLint & Prettier for React & TypeScript
지식물원
지식물원
지식이 자라는 식물원!
  • 지식물원
    지식물원
    지식물원
  • 전체
    오늘
    어제
    • 분류 전체보기 (510)
      • 🎨 프론트엔드 공부 (247)
        • JS & TS (86)
        • HTML & CSS (22)
        • React & Next (49)
        • Vue & Nuxt (22)
        • 기타 (68)
      • 🤓 기술 학습 & 공부 기록 (116)
        • Node.js (0)
        • Python (37)
        • 백엔드 (0)
        • 딥러닝 (1)
        • 컴퓨터 일반 (72)
        • 개발 인프라 (6)
      • 👨‍💻 프로젝트 경험 (6)
        • Work (0)
        • Toy (6)
      • ⚙️ 개발 팁 & 노하우 (21)
        • 프론트엔드 (6)
        • 기타 (15)
      • ☕️ 커리어 & 인터뷰 준비 (88)
        • 코딩 테스트 (88)
      • 📰 기술 트렌드 & 생각 정리 (4)
      • 📚 기타 (25)
        • 마케팅 (15)
        • 비개발서적 (10)
  • 블로그 메뉴

    • 태그
  • 링크

  • 공지사항

    • 모바일 접속 시 코드 하이라이팅 깨질 때
  • 인기 글

  • hELLO· Designed By정상우.v4.10.3
지식물원
[Gatsby & Contentful] sourcing rich text from Contenful
상단으로

티스토리툴바