Content management system 未在Sanity CMS的前端应用文本突出显示

Content management system 未在Sanity CMS的前端应用文本突出显示,content-management-system,sanity,headless-cms,Content Management System,Sanity,Headless Cms,我试图创建一个博客使用健全无头CMS和反应的前端 我做了一个用于突出显示文本的装饰器。如下图所示,在编辑器中,高亮显示的文本具有黄色背景色。 但是,我没有在我的React前端看到黄色高亮显示 我错过了什么 代码片段如下所示: sanityblog/schemas/blockContent.js import React from "react"; /** * This is the schema definition for the rich text fields u

我试图创建一个博客使用健全无头CMS和反应的前端

我做了一个用于突出显示文本的装饰器。如下图所示,在编辑器中,高亮显示的文本具有黄色背景色。 但是,我没有在我的React前端看到黄色高亮显示

我错过了什么

代码片段如下所示: sanityblog/schemas/blockContent.js

import React from "react";

/**
 * 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'
 *  }
 */

const highlightRender = (props) => (
  <span style={{ backgroundColor: "yellow" }}>{props.children}</span>
);

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: "Highlight",
            value: "highlight",
            blockEditor: {
              icon: () => "H",
              render: highlightRender,
            },
          },
        ],
        // 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",
              },
              {
                title: "Open in new tab",
                name: "blank",
                description: "Read https://css-tricks.com/use-target_blank/",
                type: "boolean",
              },
            ],
          },
        ],
      },
    },
    // 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 },
    },

    {
      type: "code",
    },
  ],
};


您需要序列化数据

您已经在代码编辑器窗口中这样做了,在当前的序列化程序中,您说“如果类型是代码,请运行此组件”

您也需要为语法荧光笔执行此操作,也许类似的操作也可以(尚未测试)

标记:{
突出显示:({children})=>{
返回;
},
},

您需要序列化数据。这是什么意思?另外,这个突出显示组件是什么?我如何使用它?突出显示组件只是我制作的一个模拟组件。在那里,您可以运行需要突出显示的代码。如果愿意,您也可以只使用HTML标记。“序列化”数据基本上只意味着将其转换为所需的格式。Sanity只提供JSON,不提供HTML。所以你必须说“如果sanity返回highlight,就把它放在这个HTML中”。如果在SanityStudio中运行GROQ查询,您可以看到该结构。看看我这里的一个连载器,也许它能给你一些灵感:)非常感谢山姆。你让我开心。我已经为此挣扎了好几天了。我发现健全的文档令人困惑。我会回来找你帮忙的。我正在做一个理智和反应前端博客。再次感谢。
import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import sanityClient from "../client.js";
import BlockContent from "@sanity/block-content-to-react";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { coldarkDark } from "react-syntax-highlighter/dist/esm/styles/prism";

export default function OnePost() {
  const [postData, setPostData] = useState(null);
  const { slug } = useParams();

  const serializers = {
    types: {
      code: (props) => (
        <SyntaxHighlighter
          language={props.node.language}
          style={coldarkDark}
          showLineNumbers
          lineNumberStyle={{
            padding: "0 5px 0 0",
            fontSize: 14,
            borderRight: "1.5px solid darkgray",
            marginRight: "10px",
          }}
        >
          {props.node.code}
        </SyntaxHighlighter>
      ),
    },
  };

  useEffect(() => {
    sanityClient
      .fetch(
        `*[slug.current == $slug]{
          title,
          slug,
          mainImage{
            asset->{
              _id,
              url
             }
           },
         body,
        "name": author->name,
        "authorImage": author->image
       }`,
        { slug }
      )
      .then((data) => setPostData(data[0]))
      .catch(console.error);
  }, [slug]);

  if (!postData) return <div>Loading...</div>;

  return (
    <div className="col-11 col-sm-10 col-md-6 mx-auto mt-5">
      <div>
        <h1 className="font-weight-bold">{postData.title}</h1>
        <div>
          <h6 className="text-secondary">{postData.name}</h6>
        </div>
      </div>
      <div className="text-muted">
        <BlockContent
          blocks={postData.body}
          projectId={sanityClient.projectId}
          dataset={sanityClient.dataset}
          serializers={serializers}
        />
      </div>
    </div>
  );
}

marks: {
  highlight: ({ children }) => {
    return <HighLightComponent children={children} />;
  },
},