【问题标题】:How to fetch content from rich text field Contentful如何从富文本字段中获取内容
【发布时间】:2021-06-19 09:26:39
【问题描述】:

我正在一个测试项目中尝试它。 这里来自 Index.js

const IndexPage = ({data: {posts}}) => {
  return (
  <div>
      <h1>Test Page</h1>
      {posts.edges.map(({node}) => (
        <Card key={node.id} node={node} />
      ))}
  </div>
  )
}

export default IndexPage;

export const query = graphql`
query {
  posts: allContentfulPost {
    edges {
      node {
        id
        title
        slug
        content{
           content
         }
        body{
           raw
         }
      }
    }
  }
}
`

我已将您建议的代码复制到我的模板组件 card.js 中。这是我尝试渲染富文本字段的时候。

import { BLOCKS, MARKS } from "@contentful/rich-text-types"
import { renderRichText } from "gatsby-source-contentful/rich-text"

    const Bold = ({ children }) => <span className="bold">{children}</span>
    const Text = ({ children }) => <p className="align-center">{children}</p>
    let richTextField;

    const options = {
      renderMark: {
        [MARKS.BOLD]: text => <Bold>{text}</Bold>,
        },
      renderNode: {
        [BLOCKS.PARAGRAPH]: (node, children) => <Text>{children}</Text>,
        [BLOCKS.EMBEDDED_ASSET]: node => {
            return (
              <>
                <h2>Embedded Asset</h2>
                <pre>
                  <code>{JSON.stringify(node, null, 2)}</code>
                </pre>
              </>
            )
          },
        },
    }
    

function Card({ node }) {
    const { body } = node.body
  
    return (
            <article style={{backgroundColor:"red"}}>
               {/* <Img fluid={images[0].FluidObject} alt=""/> */}
               {/* <a href={`/posts/${node.slug}`}>
                   <h2>{node.title}</h2>
               </a>
               <p>
               {node.content.content} 
               </p> */}
               <div>{body && renderRichText(richTextField, options)}</div>
            </article>   
    ) 
  }

export default Card;

但它不会渲染出richTextField。首先它抱怨richTextField 未定义,所以我在文件开头添加了let richTextField

【问题讨论】:

标签: gatsby contentful richtext


【解决方案1】:

像这样使用它:

import { BLOCKS, MARKS } from "@contentful/rich-text-types"
import { renderRichText } from "gatsby-source-contentful/rich-text"

const Bold = ({ children }) => <span className="bold">{children}</span>
const Text = ({ children }) => <p className="align-center">{children}</p>

const options = {
  renderMark: {
    [MARKS.BOLD]: text => <bold>{text}</bold>,
  },
  renderNode: {
    [BLOCKS.PARAGRAPH]: (node, children) => <Text>{children}</Text>,
    [BLOCKS.EMBEDDED_ASSET]: node => {
      return (
        <>
          <h2>Embedded Asset</h2>
          <pre>
            <code>{JSON.stringify(node, null, 2)}</code>
          </pre>
        </>
      )
    },
  },
}

function BlogPostTemplate({ data }) {
  const { bodyRichText } = data.contentfulBlogPost

  return <div>{bodyRichText && renderRichText(richTextField, options)}</div>
}

options 对象最终是一堆函数,它们根据您的项目逻辑返回自定义标记(即:&lt;bold&gt;{text}&lt;/bold&gt;,当富文本具有粗体样式等时)因此,您只需要渲染它使用:

renderRichText(richTextField, options)

在您想要呈现内容的组件中,理想情况下,在模板中。

更多详情请查看plugin's docs

【讨论】:

  • options 在哪里?您需要在打印之前将查询的数据传递到选项对象中。
  • 这就是我感到困惑的地方。我应该用我查询的数据替换childrentext吗?我在哪里使用const Boldconst Text。我需要一个指南,告诉我这段代码的每一部分是做什么的^_^ @FerranBuireu
  • 哦,我现在明白了,是的,options 就在 function Card 之前。没有显示它,因为它抱怨我的问题中有太多代码。
  • 放到渲染时需要解析内容的组件(函数)里面。您只需要在每种类型的内容中返回您想要的任何内容:[MARKS.BOLD]: text =&gt; &lt;div&gt;{text}&lt;/div&gt;, div
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多