【问题标题】:GraphQL to Schema MarkupGraphQL 到模式标记
【发布时间】:2020-02-04 11:30:01
【问题描述】:

我正在使用 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 检索的图像,并返回它们的 publicURL:

{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 给我的内容转换为有效的模式标记。

【问题讨论】:

    标签: json graphql gatsby structured-data


    【解决方案1】:

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

    let schemaOrgImages = [];
    data.allFile.edges.forEach(node => schemaOrgImages.push(node.publicURL));
    
    const schemaOrgJSONLD = [
      {
        "@context": "https://schema.org",
        "@type": "EventVenue",
        image: schemaOrgImages
      }
    ]
    

    可能还有另一种方法可以做到这一点,但此方法适用于 schemaOrg 验证。

    【讨论】:

      猜你喜欢
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      • 2016-07-18
      • 2019-05-10
      • 2021-10-03
      • 2021-06-13
      • 1970-01-01
      相关资源
      最近更新 更多