-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-node.js
206 lines (164 loc) · 6.59 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
const _ = require('lodash')
const { loadDynamicMenusFromNodes } = require('./dynamic-menu')
const createMenuNode = (menu, parentMenuId, index) => {
const identifier = menu.identifier || `${parentMenuId}-${index}`
return {
...(menu.data || {}),
id: `menu-${identifier}`,
identifier: identifier,
title: menu.title,
weight: menu.weight,
url: menu.url
}
}
const createParentMenuNode = (menuId) => {
return {
id: `menu-${menuId}`,
identifier: menuId
}
}
const menuComparator = (indexByMenuIdentifier, staticMenuIdentifiers) => {
return (menu1, menu2) => {
// compare weights
if (menu1.weight != undefined && menu2.weight != undefined) {
if (menu1.weight < menu2.weight) {
return -1
} else if (menu1.weight > menu2.weight) {
return 1
}
} else if (menu1.weight == undefined && menu2.weight != undefined) {
return 1
} else if (menu1.weight != undefined && menu2.weight == undefined) {
return -1
}
// static menus come first
if (!staticMenuIdentifiers.has(menu1.identifier) &&
staticMenuIdentifiers.has(menu2.identifier)) {
return -1
} else if (staticMenuIdentifiers.has(menu1.identifier) &&
!staticMenuIdentifiers.has(menu2.identifier)) {
return 1
}
// fallback on provided ordering of static and dynamic menus
return indexByMenuIdentifier.get(menu1.identifier) -
indexByMenuIdentifier.get(menu2.identifier)
}
}
const connectNodes = (childrenByMenuIdentifier, menuNodeByMenuIdentifier, pluginActions) => {
const { createNode, createNodeId, getNode, createContentDigest } = pluginActions
// Parent all-encompassing menus node
let menusNode = {
id: createNodeId(`menus`),
internal: {
type: `Menus`,
contentDigest: createContentDigest(childrenByMenuIdentifier),
}
}
childrenByMenuIdentifier.forEach((children, menuId) => {
let parentNode = menuNodeByMenuIdentifier.get(menuId)
if (!parentNode) {
parentNode = createParentMenuNode(menuId)
}
parentNode.items___NODE = children.map(childMenu => childMenu.id)
children.forEach(childMenu => {
const nodeContent = {
...childMenu,
menu___NODE: parentNode.id
}
createNode({
...nodeContent,
internal: {
type: `Menu`,
contentDigest: createContentDigest(nodeContent)
}
})
})
const parentNodeContent = {
...parentNode,
menu___NODE: _.get(getNode(parentNode.id), `menu___NODE`),
}
createNode({
...parentNodeContent,
internal: {
type: `Menu`,
contentDigest: createContentDigest(parentNodeContent),
}
})
menusNode[`${menuId}___NODE`] = parentNode.items___NODE
})
createNode(menusNode)
}
const createMenusObject = (childrenByMenuIdentifier) => {
const resolveChildren = (children) => {
return children.map(child => Object.assign(
_.omit(child, ['items___NODE']),
{
'items': resolveChildren(childrenByMenuIdentifier.get(child.identifier) || [])
}
));
};
const menus = {};
childrenByMenuIdentifier.forEach((children, menuId) => {
menus[menuId] = resolveChildren(children);
});
return menus;
}
const injectToPages = (pageContextProperty, menus, {getNodesByType, createPage, deletePage}) => {
getNodesByType(`SitePage`).forEach(pageNode => {
_.set(pageNode, `context.${pageContextProperty}`, menus);
});
};
exports.onPreExtractQueries = ({ createContentDigest, createNodeId, getNodesByType, getNode, reporter, actions }, pluginOptions) => {
const { createNode, createPage, deletePage } = actions
const pluginActions = { createContentDigest, createNodeId, getNodesByType, getNode, createNode, createPage, deletePage }
if (!_.get(pluginOptions, `sourceUrlPath`)) {
reporter.panicOnBuild(
`[gatsby-plugin-menus] The 'sourceUrlPath' plugin option is required.`
)
return
}
let childrenByMenuIdentifier = new Map()
let menuNodeByMenuIdentifier = new Map()
let indexByMenuIdentifier = new Map()
let staticMenuIdentifiers = new Set()
let allMenus = []
if (_.get(pluginOptions, `menus`)) {
allMenus.push(pluginOptions.menus)
}
const dynamicMenuLoader = pluginOptions.menusLoader ||
loadDynamicMenusFromNodes(_.get(pluginOptions, `sourceNodeType`) || `MarkdownRemark`,
_.get(pluginOptions, `sourceDataPath`) || `frontmatter`,
_.get(pluginOptions, `sourceUrlPath`))
const dynamicMenus = dynamicMenuLoader({ getNodesByType, getNode })
if (dynamicMenus) {
allMenus.push(dynamicMenus)
}
allMenus.forEach(menus => {
Object.keys(menus).forEach(menuId => {
menus[menuId].forEach((menu, index) => {
if (!childrenByMenuIdentifier.get(menuId)) {
childrenByMenuIdentifier.set(menuId, [])
}
const menuNode = createMenuNode(menu, menuId, index)
if (menuNodeByMenuIdentifier.has(menuNode.identifier)) {
reporter.panicOnBuild(`[gatsby-plugin-menus] Duplicate menu identifier: ${menuNode.identifier}`)
}
childrenByMenuIdentifier.get(menuId).push(menuNode)
menuNodeByMenuIdentifier.set(menuNode.identifier, menuNode)
indexByMenuIdentifier.set(menuNode.identifier, index)
staticMenuIdentifiers.add(menuNode.identifier)
})
})
})
// Sort all children
childrenByMenuIdentifier.forEach((children, menuId) => {
children.sort(menuComparator(indexByMenuIdentifier, staticMenuIdentifiers))
})
// Create all menu item nodes with proper relationships
connectNodes(childrenByMenuIdentifier, menuNodeByMenuIdentifier, pluginActions)
const pageContextProperty = _.get(pluginOptions, `pageContextProperty`);
if (!_.isEmpty(pageContextProperty)) {
const menus = createMenusObject(childrenByMenuIdentifier);
injectToPages(pageContextProperty, menus, pluginActions);
}
}