-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
73 lines (67 loc) · 1.97 KB
/
gatsby-node.js
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
const { createFilePath } = require(`gatsby-source-filesystem`);
const path = require('path');
exports.onCreateNode = ({ node, getNode, actions }) => {
//called when node created/updated
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` });
createNodeField({
//adds slug to pages graphql query -> allMarkdownRemark { edges { node { fields { slug }}}}
node,
name: `slug`,
value: slug,
});
}
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(
`
{
allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/content/posts/" }, frontmatter: { published: { eq: true } } }
limit: 2000
) {
edges {
node {
fields {
slug
}
}
}
}
tagsGroup: allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/content/posts/" }, frontmatter: { published: { eq: true } } }
limit: 2000
) {
group(field: frontmatter___tags) {
fieldValue
}
}
}
`
);
// console.log('NODE', result.data.allMarkdownRemark.edges);
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: `blog${node.fields.slug}`,
component: path.resolve(`./src/templates/blog-post.js`),
context: {
// Data passed to context is available in page queries as GraphQL vars.
// (when we query data it will set $slug var auto)
slug: node.fields.slug,
},
});
});
const tags = result.data.tagsGroup.group;
const tagTemplate = path.resolve('src/templates/tags.js');
tags.forEach((tag) => {
createPage({
path: `/tags/${tag.fieldValue}/`,
component: tagTemplate,
context: {
tag: tag.fieldValue,
},
});
});
};