首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理智与反应BlockContent只显示纯文本

理智与反应BlockContent只显示纯文本
EN

Stack Overflow用户
提问于 2021-12-22 16:39:43
回答 1查看 790关注 0票数 0

好的,我正在为我的投资组合网站做一个博客,我刚刚开始工作直到我意识到,当我在“理智工作室”中对我的博客文章进行样式设计时,这些风格不会转移到实际的网站上。我在网站上看到的都是纯文本。这也许是个愚蠢的问题,但我不知道我做错了什么。

这是我的SinglePost.js文件:

代码语言:javascript
复制
import { useState, useEffect } from "react";
import { useParams, Link } from "react-router-dom";
import client from "../../client";
import BlockContent from "@sanity/block-content-to-react";
import Header from "../Header";

export default function SinglePost() {
    const [singlePost, setSinglePost] = useState([])
    const [isLoading, setIsLoading] = useState(true)
    const { slug } = useParams()

    useEffect(() => {
        client
          .fetch(
            `*[slug.current == "${slug}"] {
            title,
            body,
            mainImage {
              asset -> {
                _id,
                url
              },
              alt
            }
          }`
          )
          .then((data) => setSinglePost(data[0]))
        setIsLoading(false)
    }, [slug])

    const serializers = {
        types: {
          code: (props) => (
            <pre data-language={props.node.language}>
              <code>{props.node.code}</code>
            </pre>
          ),
        },
      }

    return (
        <div className = "bg-gray-100 dark:bg-zinc-900">
            <Header />
            {isLoading ? ( <h1>Loading...</h1> ) : (
                <section className = "p-5 pb-20 lg:mx-28 md:mx-16 sm:mx-8">
                    <h1 className = "title mb-20">{singlePost.title}</h1>
                    <div className = "flex items-center justify-center">
                        {singlePost.mainImage && singlePost.mainImage.asset && (
                            <img src = {singlePost.mainImage.asset.url} alt = {singlePost.title} title = {singlePost.title} className = "rounded-xl shadow-xl dark:shadow-gray-100/10" />
                            )}
                    </div>
                    <p className = "paragraph mt-5 mb-5">By Brandon Pyle</p>
                    <div className="">
                        <BlockContent serializers={serializers} blocks={singlePost.body} projectId="2hp9gld0" dataset="production" />
                    </div>
                    <button>
                        <Link to = "/blog" className = "button">Read more articles</Link>
                    </button>
                </section>
            )}
        </div>
    )
}

这是我的blockContent.js文件:

代码语言:javascript
复制
/**
 * This is the schema definition for the rich text fields used for
 * for this blog studio. When you import it in schemas.js it can be
 * reused in other parts of the studio with:
 *  {
 *    name: 'someName',
 *    title: 'Some title',
 *    type: 'blockContent'
 *  }
 */
export default {
  title: 'Block Content',
  name: 'blockContent',
  type: 'array',
  of: [
    {
      title: 'Block',
      type: 'block',
      // Styles let you set what your user can mark up blocks with. These
      // correspond with HTML tags, but you can set any title or value
      // you want and decide how you want to deal with it where you want to
      // use your content.
      styles: [
        {title: 'Normal', value: 'normal'},
        {title: 'H1', value: 'h1'},
        {title: 'H2', value: 'h2'},
        {title: 'H3', value: 'h3'},
        {title: 'H4', value: 'h4'},
        {title: 'Quote', value: 'blockquote'},
      ],
      lists: [{title: 'Bullet', value: 'bullet'}],
      // Marks let you mark up inline text in the block editor.
      marks: {
        // Decorators usually describe a single property – e.g. a typographic
        // preference or highlighting by editors.
        decorators: [
          {title: 'Strong', value: 'strong'},
          {title: 'Emphasis', value: 'em'},
          {title: 'Code', value: 'code'},
          {title: 'Highlight', value: 'highlight'},
        ],
        // Annotations can be any object structure – e.g. a link or a footnote.
        annotations: [
          {
            title: 'URL',
            name: 'link',
            type: 'object',
            fields: [
              {
                title: 'URL',
                name: 'href',
                type: 'url',
              },
            ],
          },
        ],
      },
    },
    // You can add additional types here. Note that you can't use
    // primitive types such as 'string' and 'number' in the same array
    // as a block type.
    {
      type: 'image',
      options: {hotspot: true},
    },
  ],
}

您可以在这里找到完整的源代码:https://github.com/bpyle02/portfolio,如果您想看到一个有关错误的活生生的例子,请查看我写的这篇博文,它的目的是有不同的标题、项目列表等等:https://brandonpyle.netlify.app/blog/how-to-properly-write-a-github-readme

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-26 19:40:36

我找出了问题的原因。因为我在这个项目中使用的是TailwindCSS,所以我必须安装Tailwind字体插件,才能使样式对块内容正确工作。

要安装这个插件,请在控制台中键入npm install -D @tailwindcss/typography,然后将require('@tailwindcss/typography'),添加到tailwind.config.js文件的plugins部分。

然后将‘散文’类添加到围绕BlockContent标记的div中,如下所示:

代码语言:javascript
复制
<div className="prose">
    <BlockContent blocks = {singlePost.body} />
</div>

如果需要更多帮助,请参阅尾风文档中的本教程:https://tailwindcss.com/docs/typography-plugin

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70452334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档