GraphQL 到模式标记

GraphQL to Schema Markup

我正在使用 Gatsby 构建网站,并尝试设置一些结构化数据以帮助提高搜索可见性。我需要帮助的一点是 image 数组中有多个图像。这是我想要的示例:

const schemaOrgJSONLD = [
  {
    "@context": "https://schema.org",
    "@type": "EventVenue",
    image: [
      "https://example.com/photos/1x1/photo.jpg",
      "https://example.com/photos/4x3/photo.jpg",
      "https://example.com/photos/16x9/photo.jpg",
    ]
  }
]

我有 10 张图像,我可以使用 Graphql 检索这些图像,返回它们的公共URL:

{data.allFile.edges.map(({ node }, i) => node.publicURL)}

这给了我这样的输出:

/static/venue-001.jpg
/static/venue-002.jpg
/static/venue-003.jpg
...

我需要做的是将这些返回的 URL 转换为 Google 将接受的架构标记格式。所以如上所述,正确的输出是:

const schemaOrgJSONLD = [
  {
    "@context": "https://schema.org",
    "@type": "EventVenue",
    image: [
      "https://example.com/static/venue-001.jpg",
      "https://example.com/static/venue-002.jpg",
      "https://example.com/static/venue-002.jpg",
    ]
  }
]

当涉及到多个图像时,我基本上不知道如何将 graphql 给我的内容转换为有效的模式标记。

在我这边,我在我的 schemaOrg 声明之前创建了一个数组:

let schemaOrgImages = [];
data.allFile.edges.forEach(node => schemaOrgImages.push(node.publicURL));

const schemaOrgJSONLD = [
  {
    "@context": "https://schema.org",
    "@type": "EventVenue",
    image: schemaOrgImages
  }
]

也许还有另一种方法,但此方法适用于 schemaOrg 验证。