默认情况下,我的 Gatsby 网址类似于 2018-09-06-hexagon
有什么办法可以让他们变成/blog/2018/09/06/hexagon
?
这是我的 gatsby-node.js
的相关部分文件:
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
const blogPost = path.resolve('./src/templates/blog-post.js')
resolve(
graphql(
`
{
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
// Create blog posts pages.
const posts = result.data.allMarkdownRemark.edges
_.each(posts, (post, index) => {
const previous =
index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
createPage({
path: post.node.fields.slug,
component: blogPost,
context: {
slug: post.node.fields.slug,
previous,
next,
},
})
})
})
)
})
}
最佳答案
您可以更新博客文章 frontmatter 中的 slug 以在您想要的位置包含斜杠,或者您可以转换 createPages
中的 slug。 :
// Create blog posts pages.
const posts = result.data.allMarkdownRemark.edges
_.each(posts, (post, index) => {
const previous =
index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
const blogPostPath =
`/blog/${post.node.fields.slug.replace(/-/g, "/")}`
createPage({
path: blogPostPath,
component: blogPost,
context: {
slug: post.node.fields.slug,
postPath: blogPostPath,
previous,
next,
},
})
})
关于gatsby - 如何更改 Gatsby 博客文章页面的生成路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52655149/