-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
176 lines (152 loc) · 3.85 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
const fs = require(`fs`)
const path = require(`path`)
const mkdirp = require(`mkdirp`)
const crypto = require(`crypto`)
const Debug = require(`debug`)
const { urlResolve } = require(`gatsby-core-utils`)
const debug = Debug(`gatsby-theme-notes`)
// These are customizable theme options we only need to check once
let basePath
let contentPath
// These templates are simply data-fetching wrappers that import components
const NoteTemplate = require.resolve(`./src/templates/note`)
const NotesTemplate = require.resolve(`./src/templates/notes`)
exports.onPreBootstrap = ({ store }, themeOptions) => {
const { program } = store.getState()
basePath = themeOptions.basePath || `/`
contentPath = themeOptions.contentPath || `content/notes`
const dirs = [path.join(program.directory, contentPath)]
dirs.forEach(dir => {
debug(`Initializing ${dir} directory`)
if (!fs.existsSync(dir)) {
mkdirp.sync(dir)
}
})
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const toNotesPath = node => {
const { dir } = path.parse(node.parent.relativePath)
return urlResolve(basePath, dir, node.parent.name)
}
const result = await graphql(`
{
site {
siteMetadata {
title
}
}
mdxPages: allMdx {
edges {
node {
id
parent {
... on File {
name
base
relativePath
sourceInstanceName
}
}
}
}
}
}
`)
if (result.errors) {
console.log(result.errors)
throw new Error(`Could not query notes`, result.errors)
}
const { mdxPages, site } = result.data
const siteTitle = site.siteMetadata.title
const notes = mdxPages.edges.filter(
({ node }) => node.parent.sourceInstanceName === contentPath
)
// Create notes pages
notes.forEach(({ node }) => {
createPage({
path: toNotesPath(node),
context: {
...node,
title: node.parent.name,
},
component: NoteTemplate,
})
})
const notesUrls = notes.map(({ node }) => toNotesPath(node))
const groupedNotes = notes.reduce((acc, { node }) => {
const { dir } = path.parse(node.parent.relativePath)
if (!dir) {
return acc
}
acc[dir] = acc[dir] || []
acc[dir].push({
pagePath: urlResolve(basePath, dir),
url: toNotesPath(node),
...node,
})
return acc
}, {})
Object.entries(groupedNotes).map(([key, value]) => {
const breadcrumbs = key.split(path.sep).reduce(
(acc, dir) => [
...acc,
{
name: dir,
url: urlResolve(basePath, dir),
},
],
[]
)
createPage({
path: urlResolve(basePath, key),
context: {
breadcrumbs,
siteTitle,
urls: value.map(v => v.url),
},
component: NotesTemplate,
})
})
createPage({
path: basePath,
context: {
urls: notesUrls,
groupedNotes,
siteTitle,
},
component: NotesTemplate,
})
}
exports.sourceNodes = (
{ actions: { createTypes, createNode }, schema },
{ basePath = `/`, homeText = `~`, breadcrumbSeparator = `/` }
) => {
// Create the Garden type to solidify the field data types
createTypes(`type NotesConfig implements Node {
basePath: String!
home: String
breadcrumbSeparator: String
}`)
// create garden data from plugin config
const notesConfig = {
breadcrumbSeparator,
basePath,
homeText,
}
createNode({
...notesConfig,
id: `gatsby-theme-notes-config`,
parent: null,
children: [],
internal: {
type: `NotesConfig`,
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(notesConfig))
.digest(`hex`),
content: JSON.stringify(notesConfig),
description: `Notes Config`,
},
})
}