J B*_*ock 1 javascript reactjs gatsby
我正在使用GatsbyJS构建个人网页。我有多个页面,两个相关的页面是项目/投资组合页面和博客页面。
我在博客页面上设置了gatsby,它从特定的文件夹中提取markdown文件,并使用模板在博客页面上显示它们。我想以类似的方式在项目/项目页面上显示我的项目。我的文件夹结构如下:
-src
-pages
-BlogPostOne
-BlogPostTwo
projects.js
blog.js
-templates
BlogPostTemplate.js
ProjectTemplate.js
-projects
-project1
-project2
Run Code Online (Sandbox Code Playgroud)
我希望项目页面从项目文件夹中获取项目降价文件并使用项目模板显示它们
博客页面从pages文件夹中获取博客文章markdown文件,并使用博客文章模板显示它们,效果很好。
我基本上使用了与捕获具有不同变量名和路径的博客文章文件相同的代码,但是它不起作用。盖茨比有可能做到这一点吗?我已经搜索了他们的文档,但找不到与我尝试执行的操作类似的操作。有没有人与盖茨比做过类似的事情?
是的,这完全有可能。
该解决方案实际上非常简单,但是需要对Gatsby的内部结构有一点了解才能弄清楚。如果您已经知道Gatsby,请在GatsbyCentral上查看此代码段。
否则,这是更详细的说明。
在gatsby-node.js文件中,您需要添加以下代码:
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators;
if (_.get(node, "internal.type") === `MarkdownRemark`) {
// Get the parent node
const parent = getNode(_.get(node, "parent"));
// Create a field on this node for the "collection" of the parent
// NOTE: This is necessary so we can filter `allMarkdownRemark` by
// `collection` otherwise there is no way to filter for only markdown
// documents of type `post`.
createNodeField({
node,
name: "collection",
value: _.get(parent, "sourceInstanceName")
});
}
};
Run Code Online (Sandbox Code Playgroud)
确保您还具有require()lodash 所需的语句:
const _ = require("lodash")
Run Code Online (Sandbox Code Playgroud)
现在确保在gatsby-config.js博客文章和项目中都有两个插件部分。确保每个name选项都有一个选项,例如:
plugins: [
{
resolve: "gatsby-source-filesystem",
options: {
name: "pages",
path: `${__dirname}/src/pages`
}
},
{
resolve: "gatsby-source-filesystem",
options: {
name: "projects",
path: `${__dirname}/src/projects`
}
},
Run Code Online (Sandbox Code Playgroud)
然后,您可以查询allMarkdownRemark集合并对该字段进行过滤collection。它将是pages或projects。
查询示例如下所示:
query {
allMarkdownRemark(filter: {fields: {collection: {eq: "pages"}}}) {
...
Run Code Online (Sandbox Code Playgroud)
希望对您有所帮助。