-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
95 lines (85 loc) · 2.77 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { createFilePath } = require(`gatsby-source-filesystem`);
const { isExportDeclaration } = require("typescript");
const projectJson = require('./src/data/projects/projectData.json');
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `src/content/posts/`, trailingSlash: false });
createNodeField({
node,
name: `slug`,
value: `/content${slug.replace(/\./gi, ' ').split(' ').join('-')}`,
});
}
};
module.exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const blogTemplate = require.resolve(`./src/templates/BlogPost.js`)
const projectTemplate = require.resolve(`./src/templates/ProjectPage.js`)
const response = await graphql(`
query MyQuery {
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`
)
if (response.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
projectJson.forEach(project => {
var path = `project/${project.name.replace(' -', '').split(' ').join('-').toLowerCase()}`;
createPage({
path: path,
component: projectTemplate,
context: {
name: project.name,
}
});
});
const posts = response.data.allMarkdownRemark.edges;
const postsPerPage = 5;
const numPages = Math.ceil(posts.length / postsPerPage);
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/blog` : `/blog/${i + 1}`,
component: require.resolve("./src/templates/BlogPagination.js"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1,
},
})
})
response.data.allMarkdownRemark.edges.forEach(edge => {
createPage({
component: blogTemplate,
path: edge.node.fields.slug,
context: {
slug: edge.node.fields.slug,
},
})
});
}
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === "build-html" || stage === "develop-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /firebase/,
use: loaders.null(),
},
],
},
})
}
}