首页 > 解决方案 > 通过 Sanity Studio 中的引用链接时,如何访问资产的 URL?

问题描述

我想在 Sanity Studio 中上传 PDF,然后在主站点内容中链接到这些 PDF。

我在 Sanity Studio 中的 simpleBlockContent 输入中添加了对包含“文件”字段的文档的引用。

我为 PDF 创建了一个文档架构:

export default {
  title: "PDF Upload",
  name: "pdfDocument",
  type: "document",
  fields: [
    {
      name: "title",
      type: "string",
      title: "Title",
      description: "This title will be used as a caption for the download.",
    },
    {
      name: "pdfFile",
      type: "file",
      title: "PDF File",
      options: {
        accept: ".pdf",
      },
      validation: (Rule) => Rule.required(),
      description: "Note that the file name will be visible to end users downloading the file.",
    },
  ],
};

我试图在我的输入组件的架构中引用它:

export default {
  title: "Simple Block Content",
  name: "simpleBlockContent",
  type: "array",
  of: [
    {
      title: "Block",
      type: "block",
      styles: [],
      marks: {
        annotations: [
          {
            name: "pdfLink",
            type: "object",
            title: "PDF download link",
            fields: [
              {
                name: "pdfReference",
                type: "reference",
                title: "PDF Document",
                to: [{ type: "pdfDocument" }],
              },
            ],
          },
        ],
      },
    },
  ],
};

但是,当我在前端将 pdfLink 添加到我的 serializers.js 时,从处理所有其他页面内容的 _rawContent graphql 查询传递给它的数据中没有任何类似文件链接的内容。

如何访问构建链接到已上传资产的 URL 所需的信息?

标签: sanity

解决方案


根据文档,我还没有在序列化程序中执行此操作,但看起来资产 URL 应该可以在返回的文档中访问

退回资产文件示例:

{
  "_id": "image-abc123_0G0Pkg3JLakKCLrF1podAdE9-538x538-jpg",
  "_type": "sanity.imageAsset", // type is prefixed by sanity schema
  "assetId": "0G0Pkg3JLakKCLrF1podAdE9",
  "path": "images/myproject/mydataset/abc123_0G0Pkg3JLakKCLrF1podAdE9-538x538.jpg",
  "url": "https://cdn.sanity.io/images/myproject/mydataset/abc123_0G0Pkg3JLakKCLrF1podAdE9-538x538.jpg",
  "originalFilename": "bicycle.jpg",
  "size": 2097152, // File size, in bytes
  "metadata": {
    "dimensions": {
      "height": 538,
      "width": 538,
      "aspectRatio": 1.0
    },
    "location":{ // only present if the original image contained location metadata
      "lat": 59.9241370,
      "lon": 10.7583846,
      "alt": 21.0
    }
  }
}

推荐阅读