如何在上传到 Algolia 之前更改 slug
How to change slug before uploading to Algolia
我正在努力将 Algolia 搜索应用到我的 Gatsby 网站中。我需要正确格式化我的内容段,以便 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;
使用你的transformer
:
const queries = [
{
query: iconQuery,
transformer: ({ data }) =>
data.allContentfulIcon.edges.map(({ node }) => {
node.slug = /design-resources/icons/${node.slug}/
return node;
}),
},
];
module.exports = queries;
我正在努力将 Algolia 搜索应用到我的 Gatsby 网站中。我需要正确格式化我的内容段,以便 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;
使用你的transformer
:
const queries = [
{
query: iconQuery,
transformer: ({ data }) =>
data.allContentfulIcon.edges.map(({ node }) => {
node.slug = /design-resources/icons/${node.slug}/
return node;
}),
},
];
module.exports = queries;