Sanity CMS, referencing arrays within a document

Viewed 423

My goal is to have a custom internalLink mark that is able to reference not just the post (which is easy enough since its a type: document) but also reference name: sections which returns array of section.js. That way editors can link to any section within a post document. And ideally the editors can also reach other post sections as well, not just the current one open.

I want to be able to reference the following collection of section.js in the reference dropdown:

enter image description here enter image description here

Now I understand reference only works for type: document. Can I make it work for the children (array or object) of type: post?

My custom 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" }],
    },
  ],
}

section.js

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

    },
  ],
}

Appreciate any help/pointers I can get :)

0 Answers
Related