-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
executable file
·115 lines (105 loc) · 3.15 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
const path = require('path')
const StaticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin')
const xmldom = require('xmldom')
const {syncToAlgolia} = require('./algoliasync')
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators
let slug
if (node.internal.type === `MarkdownRemark`) {
const fileNode = getNode(node.parent)
const parsedFilePath = path.parse(fileNode.relativePath)
const splittedDir = parsedFilePath.dir.split('/')
// remove /backend /frontend /graphql
if (splittedDir.length === 2) {
slug = `/${splittedDir[1]}/${parsedFilePath.name}/`
} else if (parsedFilePath.name !== `index` && parsedFilePath.dir !== ``) {
slug = `/${parsedFilePath.dir}/${parsedFilePath.name}/`
} else if (parsedFilePath.dir === ``) {
slug = `/${parsedFilePath.name}/`
} else {
slug = `/${parsedFilePath.dir}/`
}
// Add slug as a field on the node.
createNodeField({ node, fieldName: `slug`, fieldValue: slug })
}
}
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators
return new Promise((resolve, reject) => {
const pages = []
const tutorials = path.resolve('src/templates/Tutorials.tsx')
// Query for all markdown "nodes" and for the slug we previously created.
resolve(
graphql(
`{
allMarkdownRemark {
edges {
node {
frontmatter {
title
}
fields {
slug
}
excerpt(pruneLength: 1000)
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
// Create blog posts pages.
result.data.allMarkdownRemark.edges.forEach(edge => {
createPage({
path: edge.node.fields.slug, // required
component: tutorials,
context: {
slug: edge.node.fields.slug,
},
})
})
if (process.env.NODE_ENV === 'production') {
try {
syncToAlgolia(result.data)
} catch (e) {
console.log('Warning: Algolia Error')
}
}
return
})
)
})
}
exports.modifyWebpackConfig = function modifyWebpackConfig({config, stage}) {
config.removeLoader('md')
config.loader('md', {
test: /\.md$/,
loader: 'babel-loader!reactdown/webpack'
})
if (stage === 'build-html') {
const pages = config._config.plugins[0].paths
config._config.plugins[0] = new StaticSiteGeneratorPlugin({
entry: `render-page.js`,
paths: pages,
globals: {
DOMParser: xmldom.DOMParser,
NodeList: xmldom.NodeList,
atob: function atob(str) {
return new Buffer(str, 'base64').toString('binary');
},
location: {
pathname: ''
},
document: {
addEventListener: () => null,
removeEventListener: () => null
}
}
})
}
return config
}