-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.esm.js
More file actions
75 lines (72 loc) · 2.16 KB
/
gatsby-node.esm.js
File metadata and controls
75 lines (72 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const path = require(`path`);
const { createFilePath } = require(`gatsby-source-filesystem`);
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: 'portfolio/categories' });
createNodeField({
node,
name: `slug`,
value: `/portfolio${slug}`,
});
}
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(`
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
frontmatter {
imgName
imgDescription
category
image
}
}
}
}
}
`);
const allImages = Array.from(
result.data.allMarkdownRemark.edges.filter(({ node }) =>
node.fields.slug.startsWith('/portfolio/images')
),
({ node }) => node.frontmatter
);
const allCategories = Array.from(
result.data.allMarkdownRemark.edges.filter(
({ node }) =>
node.fields.slug.startsWith('/portfolio') &&
!node.fields.slug.startsWith('/portfolio/images')
),
({ node }) => ({
slug: node.fields.slug,
name:
node.fields.slug[node.fields.slug.length - 1] === '/'
? node.fields.slug.replace('/portfolio/', '').slice(0, -1)
: node.fields.slug.replace('/portfolio/', ''),
})
);
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
if (!node.fields.slug.startsWith('/portfolio/images')) {
const categoryName =
node.fields.slug[node.fields.slug.length - 1] === '/'
? node.fields.slug.replace('/portfolio/', '').slice(0, -1)
: node.fields.slug.replace('/portfolio/', '');
createPage({
path: node.fields.slug,
component: path.resolve(`./src/templates/PortfolioTemplate.tsx`),
context: {
images: allImages.filter((image) => image.category === categoryName),
categories: allCategories,
currentCategoryName: categoryName,
},
});
}
});
};