-
Notifications
You must be signed in to change notification settings - Fork 3
/
gatsby-node.js
208 lines (184 loc) · 5.37 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
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const express= require('express');
const webpackLodashPlugin = require('lodash-webpack-plugin')
const dirTree = require('directory-tree')
const config = require('./data/SiteConfig')
const normalisePath = require('./src/utils/normalise-path')
const redirects = require('./data/redirects')
function capitalizeWords(words) {
return words
.split(' ')
.map(capitalizeWord)
.join(' ')
}
function capitalizeWord(word) {
const [head, ...tail] = word.split('')
return [head.toUpperCase(), tail.join('')].join('')
}
function convertNames(name) {
return name.replace(/.md/, '').replace(/(-|_)/g, ' ')
}
function parseTree(nodes, pathToReplace) {
const item = {}
const { path: nodePath, name, type, children } = nodes
item.title = capitalizeWords(convertNames(name))
if (type === 'directory') {
const rootNode = children.find(child =>
child.path.includes(`${nodePath}/index.md`)
)
/* handles index.md node, uses folder name, hoists up from children */
if (rootNode) {
item.entry = `.${rootNode.path.replace(pathToReplace, '')}`
item.title = capitalizeWords(convertNames(nodePath.split('/').pop()))
}
item.links = children
.filter(child => !child.name.includes('index'))
.map(child => parseTree(child, pathToReplace))
} else {
item.entry = `.${nodePath.replace(pathToReplace, '')}`
item.links = null
}
return item
}
function writeToFile(key, content) {
fs.writeFile(
`./content/${key}/${key}-tree.json`,
JSON.stringify(content, null, 2),
function(err) {
if (err) {
return console.log(err)
}
}
)
}
exports.onPreBootstrap = () => {
const contentPath = 'content'
const tree = dirTree(`./${contentPath}`, {
extensions: /\.md/,
normalizePath: true
})
const groupedTree = _.groupBy(tree.children, 'name')
Object.keys(groupedTree).forEach(key => {
const tree = _.get(groupedTree, `${key}[0]`)
const newTree = parseTree(tree, `${contentPath}/${key}`)
writeToFile(key, newTree)
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === 'MarkdownRemark') {
const fileNode = getNode(node.parent)
const parsedFilePath = path.parse(fileNode.relativePath)
const directoryName = parsedFilePath.dir.split('/')[0]
const templateName = node.frontmatter.template || directoryName
let template = `${config.templateDir}${templateName}.js`
if (!fs.existsSync(template)) {
// eslint-disable-next-line no-console
console.warn(
'\nWARNING: No template set for ',
template,
'. Setting to default.js'
)
template = `${config.templateDir}default.js`
}
let slug = `/${parsedFilePath.dir}/`
if (parsedFilePath.name !== 'index') {
slug += `${parsedFilePath.name.replace(/_/g, '-')}/`
}
if (directoryName === 'blog') {
slug = `/blog/${parsedFilePath.name}/`
}
createNodeField({
node,
name: 'slug',
value: slug
})
createNodeField({
node,
name: 'template',
value: template
})
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect } = actions
Object.keys(redirects).forEach(fromPath => {
// With trailing slash
createRedirect({
fromPath,
isPermanent: true,
redirectInBrowser: true,
toPath: redirects[fromPath]
})
// Without trailing slash
createRedirect({
fromPath: normalisePath(fromPath),
isPermanent: true,
redirectInBrowser: true,
toPath: redirects[fromPath]
})
})
return new Promise((resolve, reject) => {
const allQuery = graphql(
`
{
allMarkdownRemark {
edges {
node {
frontmatter {
title
template
}
fields {
slug
template
}
}
}
}
}
`
)
resolve(
allQuery.then(result => {
if (result.errors) {
reject(result.errors)
}
result.data.allMarkdownRemark.edges.forEach(edge => {
createPage({
path: `${edge.node.fields.slug}`,
component: path.resolve(edge.node.fields.template),
context: {
slug: edge.node.fields.slug
}
})
})
// Create blog pages
const numberOfBlogPosts = result.data.allMarkdownRemark.edges.filter(
e => e.node.fields.slug.includes('blog')
).length
const postsPerPage = 10
const paginationCount = Math.ceil(numberOfBlogPosts / postsPerPage)
// Create paginated pages
for (let p = 0; p < paginationCount; p++) {
createPage({
path: p === 0 ? '/blog' : `/blog/${p + 1}`,
component: path.resolve('src/templates/blog-list.js'),
context: {
skip: p * postsPerPage,
limit: postsPerPage,
paginationCount,
prevPath: p === 0 ? null : `/blog/${p === 1 ? '' : p}`,
nextPath: p === paginationCount - 1 ? null : `/blog/${p + 2}`
}
})
}
})
)
})
}
exports.onCreateDevServer = ({ app }) => {
app.use(express.static('public'))
}