首页 > 解决方案 > Sanity CMS,在文档中引用数组

问题描述

我的目标是有一个自定义的 internalLink 标记,它不仅可以引用帖子(因为它很容易,因为它 a type: document)而且还可以引用name: sections返回数组的section.js. 这样,编辑者可以链接到帖子文档中的任何部分。理想情况下,编辑也可以访问其他帖子部分,而不仅仅是当前打开的部分。

我希望能够section.js在参考下拉列表中参考以下集合:

在此处输入图像描述 在此处输入图像描述

现在我明白参考仅适用于type: document. 我可以让它为 的孩子(数组或对象)工作type: post吗?

我的自定义 block.js

marks: {
        ...
        annotations: [
          {
            name: "internalLink",
            title: "Internal link",
            type: "object",
            fields: [
              {
                name: "linkto",
                type: "reference",
                title: "Link To",
                to: [{ type: "post" }],
              },
            ],
          },
          ...

post.js


export default {
  name: "post",
  title: "Document",
  type: "document",
  fields: [
    ...
    {
      name: "sections",
      title: "Sections",
      type: "array",
      of: [{ type: "section" }],
    },
  ],
}

节.js

export default {
  title: "Section",
  name: "section",
  type: "object",
  fields: [
    {
      name: "sectionTitle",
      title: "Section Title",
      type: "string",
    },
    {
      title: "Content",
      name: "content",
      type: "blockContent",

    },
  ],
}

感谢我能得到的任何帮助/指示:)

标签: javascriptsanity

解决方案


推荐阅读