【问题标题】:How to change slug before uploading to Algolia如何在上传到 Algolia 之前更改 slug
【发布时间】:2021-02-04 13:48:51
【问题描述】:

我正在努力在我的 Gatsby 网站中实施 Algolia 搜索。我需要正确格式化内容的 slug,以便 Algolia 获得正确的链接。

我的查询从 Contentful 得到的 slug 类似于 /icon-name/

我需要它来匹配从我的 Gatsby-node.js 文件路径创建的路径:/design-resources/icons/${node.slug}/

是否可以在从 Gatsby-node.js 创建后获取此路径,或者有没有办法在上传到 Algolia 之前转换下面的图标查询?

我的查询

const iconQuery = `
query iconQuery {
    allContentfulIcon {
        edges {
            node {
                id
                title
                slug
                keywords
                pngFIle {
                    fluid {
                        srcSetWebp
                    }
                  }
              }
        }
    }
}`;

Algolia 的变压器

const queries = [
  {
    query: iconQuery,
    transformer: ({ data }) =>
      data.allContentfulIcon.edges.map(({ node }) => node), // optional
  },
];

module.exports = queries;

【问题讨论】:

    标签: gatsby algolia contentful


    【解决方案1】:

    使用您的transformer

    const queries = [
      {
        query: iconQuery,
        transformer: ({ data }) =>
          data.allContentfulIcon.edges.map(({ node }) => {
            node.slug = /design-resources/icons/${node.slug}/          
          
            return node;
        }),
      },
    ];
    
    module.exports = queries;
    

    【讨论】:

    • 谢谢!在变压器中对其进行转换是有意义的。当我运行这段代码时,我收到了这个错误 node.slug=${/design-resources/icons/${node.slug}/} ^ SyntaxError: Invalid regular expression flags。我去掉了括号,它起作用了。这是我的最终代码。 node.slug = /design-resources/icons/${node.slug}/;
    • 感谢@GGlass 的见解。我已经用工作的最终代码更新了答案
    猜你喜欢
    • 1970-01-01
    • 2018-11-20
    • 2023-04-02
    • 2020-11-28
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多