Filter files based on parent dir

This commit is contained in:
LordMathis 2020-10-05 18:36:54 +02:00
parent f8e2ba3177
commit c064850324
3 changed files with 26 additions and 5 deletions

View File

@ -20,6 +20,20 @@ module.exports = {
],
},
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
name: `data`,
path: `${__dirname}/content/pages`,
},
},
{
resolve: `gatsby-transformer-remark`,
options: {
filter: node => node.sourceInstanceName === `data`,
type: `MarkdownPage`
}
},
{
resolve: `gatsby-source-filesystem`,
options: {
@ -27,11 +41,12 @@ module.exports = {
path: `${__dirname}/content/posts/`,
},
},
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-transformer-remark`,
options: {
excerpt_separator: `<!-- end -->`
filter: node => node.sourceInstanceName === `pages`,
excerpt_separator: `<!-- end -->`,
type: `BlogPost`
}
},
{
@ -43,6 +58,7 @@ module.exports = {
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-plugin-manifest`,
options: {

View File

@ -5,11 +5,17 @@ exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` })
const parent = getNode(node.parent)
createNodeField({
node,
name: `slug`,
value: slug,
})
createNodeField({
node,
name: 'collection',
value: parent.sourceInstanceName,
})
}
}
@ -19,7 +25,7 @@ exports.createPages = async ({ graphql, actions, reporter }) => {
const result = await graphql(
`
{
allMarkdownRemark(filter: {frontmatter: {draft: {ne: true}}}) {
allMarkdownRemark(filter:{frontmatter: {draft: {ne: true}}, fields: {collection: {eq: "posts"}}}) {
edges {
node {
fields {
@ -41,7 +47,6 @@ exports.createPages = async ({ graphql, actions, reporter }) => {
// Create pages for each markdown file.
const blogPostTemplate = path.resolve(`src/templates/blog-post.js`)
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
console.log(node);
createPage({
path: `/posts${node.fields.slug}`,
component: blogPostTemplate,

View File

@ -12,7 +12,7 @@ const IndexPage = () => {
query {
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
filter: {frontmatter: {draft: {ne: true}}}
filter: {frontmatter: {draft: {ne: true}}, fields: {collection: {eq: "posts"}}}
) {
edges {
node {