我有一个职位目录,如:
src
-posts
-post-a
index.md
demo.jpg
-post-b
index.md
logo.jpg
-pages
index.js
// ...
我想让目录名称post-a / post-b作为index.md的ID,我该怎么办?
这是我的gatsby-config.js
:
module.exports = {
plugins: [
{
resolve: 'gatsby-source-filesystem',
options: {
name: 'posts',
path: `${__dirname}/src/posts`,
},
},
{
resolve: 'gatsby-transformer-remark',
options: {
commonmark: true,
footnotes: true,
pedantic: true,
gfm: true,
plugins: [],
},
},
'gatsby-transformer-sharp',
'gatsby-plugin-sharp',
'gatsby-plugin-react-helmet',
],
};
但allMarkdownRemark.edges.node
没有目录信息。
您可以使用createFilePath
从功能gatsby-source-filesystem
来构建你塞并将其添加到markdownRemark.fields
使用onCreateNode
API。官方博客启动器也这样做:https : //github.com/gatsbyjs/gatsby-starter-blog/blob/04ace724603b46198665e052006031ba7e644f9d/gatsby-node.js#L53-L64
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}
现在,该条包含博客文章的目录名称。
您最初的问题:
您可以使用联合类型来访问父级,例如:
{
allMarkdownRemark {
nodes {
parent {
... on File {
dir
}
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句