【问题标题】:unordered list in contentful-rich-text rendering as P tags内容丰富的富文本呈现为 P 标记的无序列表
【发布时间】:2021-11-20 17:50:51
【问题描述】:

我在内容丰富的空间中有一个项目符号列表,但由于某种原因它显示为 P 标记。我尝试过使用 LIST_ITEM,但结果仍然相同。我只需要显示要点。我想我缺少一种富文本类型,但我不确定是哪一种?

const options = {
    renderMark: {
        [BLOCKS.LIST_ITEM]: (children) => 
            <li style={{ fontSize: '60px', fontFamily: 'Montserrat' }}>{children}</li>,
    },
};

<ul className="leading-8">
    <li>{renderRichText(data.contentfulReports.fullDescription, options)}</li>
</ul>

这是我的查询

query MyBlogs($slug: String) {
    contentfulReports(slug: { eq: $slug }) {
        title
        slug
        summary {
            summary
        }
        table {
            tableData
        }
        fullDescription {
            raw
        }
        bannerImage {
            fluid {
              src
            }
        }
    }
};

【问题讨论】:

    标签: reactjs graphql contentful


    【解决方案1】:

    尝试使用 renderNode 代替 renderMark:

    renderNode: {
          [BLOCKS.PARAGRAPH]: (node, children) => <p>{children}</p>,
          [BLOCKS.UL_LIST]: (node, children) => (
            <ul>{children}</ul>
          ),
          [BLOCKS.OL_LIST]: (node, children) => (
            <ol>{children}</ol>
          ),
          [BLOCKS.LIST_ITEM]: (node, children) => <li>{children}</li>,
    }
    

    在我们的例子中,我们查询的是 json 而不是 raw:

    fullDescription {
          json
    }
    

    并使用 documentToReactComponents 函数:

    import { documentToReactComponents } from "@contentful/rich-text-react-renderer";
    …
    documentToReactComponents(fulldescription.json, options)
    

    【讨论】:

    • 如何使用 raw 呢?解决方案还能用吗?
    • 它的工作原理是一样的,这里的主要内容是renderNode使用块和renderMark作为标记(只有粗体、代码、斜体、下划线),这在github.com/contentful/rich-text/tree/master/packages/…上有更好的解释
    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2021-12-09
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多