-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
108 lines (98 loc) · 2.7 KB
/
gatsby-node.js
File metadata and controls
108 lines (98 loc) · 2.7 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
// You can delete this file if you're not using it
const _ = require(`lodash`)
const path = require(`path`)
const slash = require(`slash`)
const LodashModuleReplacementPlugin = require("lodash-webpack-plugin")
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
const blogPostTemplate = path.resolve(`src/templates/blogTemplate.js`)
const tagPagesTemplate = path.resolve(`src/templates/template-tag-page.js`)
return graphql(`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
fields {
slug
}
frontmatter {
path
tags
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
}
const posts = result.data.allMarkdownRemark.edges
posts.forEach(({ node }) => {
createPage({
brief: node.frontmatter.brief,
author: node.frontmatter.author,
path: node.frontmatter.path,
tag: node.frontmatter.tags,
component: blogPostTemplate,
context: {
slug: node.fields.slug,
}, // additional data can be passed via context
})
})
let tags = []
result.data.allMarkdownRemark.edges.forEach(edge => {
if (_.get(edge, `node.frontmatter.tags`)) {
tags = tags.concat(edge.node.frontmatter.tags)
}
})
tags = _.uniq(tags)
tags.forEach(tag => {
const tagPath = `/tags/${_.kebabCase(tag)}/`
createPage({
path: tagPath,
component: tagPagesTemplate,
context: {
tag,
},
})
})
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `File`) {
const parsedFilePath = path.parse(node.absolutePath)
const slug = `/${parsedFilePath.dir.split(`---`)[1]}/`
createNodeField({ node, name: `slug`, value: slug })
} else if (
node.internal.type === `MarkdownRemark` &&
typeof node.slug === `undefined`
) {
const fileNode = getNode(node.parent)
createNodeField({
node,
name: `slug`,
value: fileNode.fields.slug,
})
if (node.frontmatter.tags) {
const tagSlugs = node.frontmatter.tags.map(
tag => `/tags/${_.kebabCase(tag)}/`
)
createNodeField({ node, name: `tagSlugs`, value: tagSlugs })
}
}
}
exports.onCreateWebpackConfig = ({ stage, actions }) => {
switch (stage) {
case `build-javascript`:
actions.setWebpackConfig({
plugins: [new LodashModuleReplacementPlugin()],
})
}
}