forked from dferber90/gatsby-starter-help-center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
214 lines (199 loc) · 5.99 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const slug = require("slug")
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const articlePage = require.resolve(`./src/templates/article.js`)
const collectionPage = require.resolve(`./src/templates/collection.js`)
const result = await graphql(
`
{
allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }) {
edges {
node {
id
fields {
slug
}
parent {
id
... on File {
name
sourceInstanceName
}
}
frontmatter {
title
}
}
}
}
allCollectionsYaml {
edges {
node {
id
title
fields {
slug
}
}
}
}
}
`
)
if (result.errors) {
throw result.errors
}
// Create article and collection pages.
const items = result.data.allMarkdownRemark.edges
const articles = items.filter(
(item) => item.node.parent.sourceInstanceName === "articles"
)
const collections = result.data.allCollectionsYaml.edges
articles.forEach((post, index) => {
const slug = post.node.fields.slug
createPage({
path: slug,
component: articlePage,
context: {
slug,
},
})
})
collections.forEach(({ node }, index) => {
createPage({
path: node.fields.slug,
component: collectionPage,
context: {
slug: node.fields.slug,
collectionId: node.id,
},
})
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
if (node.internal.type === `MarkdownRemark`) {
const fileNode = getNode(node.parent)
const sourceInstanceName = fileNode.sourceInstanceName
// creates slugs like
// /articles/some-heading
const value =
"articles" === sourceInstanceName
? "/" + sourceInstanceName + createFilePath({ node, getNode })
: createFilePath({ node, getNode })
actions.createNodeField({
name: `slug`,
node,
value,
})
} else if (node.internal.type === `CollectionsYaml`) {
// creates slugs like
// /collections/some-collection-name
actions.createNodeField({
name: `slug`,
node,
value: "/collections/" + slug(node.id),
})
}
}
// Checks whether "articles" containts the article located at "articlePath".
// "articlePath" must be an absolute url.
function articlesContainSpecificArticle(articles, articlePath) {
return (
Array.isArray(articles) &&
articles.some(
// compare articles by absolute file path
// use path.normalize for windows paths
(article) => path.normalize(path.resolve("data", article.file)) === path.normalize(articlePath)
)
)
}
exports.createSchemaCustomization = ({ actions, schema }) => {
const typeDefs = [
"type MarkdownRemark implements Node { fields: MarkdownRemarkFields }",
schema.buildObjectType({
name: "MarkdownRemarkFields",
fields: {
collection: {
type: "CollectionsYaml",
resolve: (source, args, context, info) => {
// determine the collection of the article based on the file path
const rootNode = context.nodeModel.findRootNodeAncestor(source)
const collection = context.nodeModel
.getAllNodes({ type: "CollectionsYaml" })
.find((collection) => {
// check fixed articles
if (
articlesContainSpecificArticle(
collection.articles,
rootNode.absolutePath
)
)
return true
// check articles of sections
if (
Array.isArray(collection.sections) &&
collection.sections.some((section) => {
return articlesContainSpecificArticle(
section.articles,
rootNode.absolutePath
)
})
)
return true
return false
})
//
return collection
},
},
section: {
type: "CollectionsYamlSections",
resolve: (source, args, context, info) => {
const rootNode = context.nodeModel.findRootNodeAncestor(source)
// determine the section of the article based on the file path
const sections = context.nodeModel
.getAllNodes({ type: "CollectionsYaml" })
.filter((collection) => Array.isArray(collection.sections))
.flatMap((collection) => collection.sections)
return sections.find((section) =>
articlesContainSpecificArticle(
section.articles,
rootNode.absolutePath
)
)
},
},
},
}),
// hard-code collection types to avoid errors when collections don't use all
// features
`
type CollectionsYaml implements Node {
sections: [CollectionsYamlSections]
articles: [CollectionsYamlArticles]
}
type CollectionsYamlArticles implements Node @infer {
file: File @fileByRelativePath
}
type CollectionsYamlSections implements Node {
id: String!
title: String!
articles: [CollectionsYamlSectionsArticles]
}
type CollectionsYamlSectionsArticles implements Node @infer {
file: File @fileByRelativePath
}
`,
// hard-code article frontmatter types to avoid erros when articles don't
// use all features
`
type MarkdownRemarkFrontmatter implements Node {
date: Date @dateformat
modifiedDate: Date @dateformat
}
`,
]
actions.createTypes(typeDefs)
}