forked from Uniswap/org-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
43 lines (39 loc) · 1.16 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
const path = require('path')
function getSlugPath(slug) {
return slug.slice(1, slug.length - 1).split('/')
}
exports.onCreateNode = ({ node, getNodesByType, actions }) => {
const { createNodeField, createParentChildLink } = actions
if (node.internal.type === 'Directory') {
// in some case the trailing slash is missing.
// Always add it and normalize the path to remove duplication
const parentDirectory = path.normalize(node.dir + '/')
const parent = getNodesByType('Directory').find(n => path.normalize(n.absolutePath + '/') === parentDirectory)
if (parent) {
node.parent = parent.id
createParentChildLink({
child: node,
parent
})
}
}
// Ensures we are processing only markdown files
if (node.internal.type === 'Mdx' || node.internal.type === 'Md') {
const slugPath = getSlugPath(node.fields.slug)
createNodeField({
node,
name: 'topLevelDir',
value: slugPath[0]
})
createNodeField({
node,
name: 'parentDir',
value: slugPath ? slugPath[2] : ' '
})
createNodeField({
node,
name: 'subDir',
value: slugPath[slugPath.length - 1]
})
}
}