Web 盖茨比·索罗斯心满意足的身体

Web 盖茨比·索罗斯心满意足的身体,web,gatsby,gatsby-plugin,Web,Gatsby,Gatsby Plugin,嘿,我在用盖茨比·斯鲁塞的《知足常乐》从《知足常乐》获取我的博客文章。我试图将正文作为富文本或json,但graphql允许我使用的唯一选项是raw,它会返回一堆对象和我不想要的文本。步骤1。确保已使用以下方法导入gatsby source contentful: npm install gatsby-source-contentful 第二步。将此项添加到导入中: import { renderRichText } from "gatsby-source-contentful/ri

嘿,我在用盖茨比·斯鲁塞的《知足常乐》从《知足常乐》获取我的博客文章。我试图将正文作为富文本或json,但graphql允许我使用的唯一选项是raw,它会返回一堆对象和我不想要的文本。

步骤1。确保已使用以下方法导入gatsby source contentful:

npm install gatsby-source-contentful
第二步。将此项添加到导入中:

import { renderRichText } from "gatsby-source-contentful/rich-text"
第三步。您的查询应该如下所示

export const query = graphql`
query($slug: String!){
    contentfulBlogPost(slug: {eq: $slug}) {
        title
        publishedDate(formatString: "MMMM Do, YYYY")
        body{
            raw
        }
    }
}`
第四步。返回{renderRichText(props.data.contentfulBlogPost.body)}

`

添加对原始数据的引用

const Blog = (props) => {
const options = {
    renderNode: {
        "embedded-asset-block": (node) => {
            const alt = "test"
            const url = props.data.contentfulBlogPost.body.references[0].fixed.src
            return <img src={url} alt={alt}/>
        }
    }
}
const博客=(道具)=>{
常量选项={
渲染节点:{
“嵌入式资产块”:(节点)=>{
const alt=“测试”
const url=props.data.contentfulBlogPost.body.references[0].fixed.src
返回
}
}
}
最后,在渲染部分:

<div>{renderRichText(props.data.contentfulBlogPost.body, options)}</div>
{renderRichText(props.data.contentfulBlogPost.body,options)}

步骤1.确保已使用以下方法导入gatsby source contentful:

npm install gatsby-source-contentful
步骤2.将此项添加到导入中:

import { renderRichText } from "gatsby-source-contentful/rich-text"
步骤3.您的查询应该如下所示

export const query = graphql`
query($slug: String!){
    contentfulBlogPost(slug: {eq: $slug}) {
        title
        publishedDate(formatString: "MMMM Do, YYYY")
        body{
            raw
        }
    }
}`
步骤4.返回{renderRichText(props.data.contentfulBlogPost.body)}

`

添加对原始数据的引用

const Blog = (props) => {
const options = {
    renderNode: {
        "embedded-asset-block": (node) => {
            const alt = "test"
            const url = props.data.contentfulBlogPost.body.references[0].fixed.src
            return <img src={url} alt={alt}/>
        }
    }
}
const博客=(道具)=>{
常量选项={
渲染节点:{
“嵌入式资产块”:(节点)=>{
const alt=“测试”
const url=props.data.contentfulBlogPost.body.references[0].fixed.src
返回
}
}
}
最后,在渲染部分:

<div>{renderRichText(props.data.contentfulBlogPost.body, options)}</div>
{renderRichText(props.data.contentfulBlogPost.body,options)}

raw
对象是在最新版本的
gatsby source contentful
中添加的一个新的“功能”

注意:请注意,盖茨比内容源插件的早期版本使用了
json
字段。该字段被替换为
raw
以提供 您可以在渲染和修复性能问题方面获得更大的灵活性

Contentful指出的“灵活性”是自定义组件的
return
语句的输出的能力,该语句将解析
raw
响应

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>
        </>
      )
    },
  },
}
​
renderRichText(node.bodyRichText, options)
) }, }, } ​ renderRichText(node.bodyRichText,选项) 上面的代码段允许您自定义每个
标记
条目的响应,根据需要添加适当的样式,并将其包装到您可能需要的任何结构中。上面的组件将允许您解析原始响应并返回正确的组件


您可以查看此答案中提供的内容文档的更多详细信息。

raw
对象是最新版本的
gatsby source Contentful
中添加的新“功能”。根据报告:

注意:请注意,盖茨比内容源插件的早期版本使用了
json
字段。这被替换为
raw
以提供 您可以在渲染和修复性能问题方面获得更大的灵活性

Contentful指出的“灵活性”是定制组件的
return
语句的输出的能力,该语句将解析
raw
响应。理想情况下,您应该有如下内容:

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>
        </>
      )
    },
  },
}
​
renderRichText(node.bodyRichText, options)
) }, }, } ​ renderRichText(node.bodyRichText,选项) 上面的代码段允许您自定义每个
标记
条目的响应,根据需要添加适当的样式,并将其包装到您可能需要的任何结构中。上面的组件将允许您解析原始响应并返回正确的组件

您可以查看此答案中提供的内容文档和