This repository has been archived by the owner on May 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
122 lines (110 loc) · 2.75 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
// core
const path = require('path')
// npm
const { createFilePath } = require('gatsby-source-filesystem')
// self
const { languages } = require('./src/i18n/locales')
exports.onCreateNode = ({ node, getNode, actions: { createNodeField } }) => {
if (node.internal.type !== 'MarkdownRemark') {
return
}
const value =
`/blog${createFilePath({
node,
getNode,
basePath: 'custom/blog'
})}`.slice(0, -4) + '/'
createNodeField({
name: 'slug',
node,
value
})
}
exports.createPages = ({ graphql, actions: { createPage } }) => {
return new Promise((resolve, reject) => {
graphql(`
{
allMarkdownRemark(filter: { frontmatter: { title: { ne: "" } } }) {
edges {
node {
fields {
slug
}
frontmatter {
language
set
}
}
}
}
}
`).then(result => {
if (!result.data || !result.data.allMarkdownRemark) {
return resolve()
}
result.data.allMarkdownRemark.edges
.map(({ node }) => node)
.forEach(node => {
languages.forEach(({ value }) => {
if (value !== node.frontmatter.language) {
return
}
createPage({
path: `/${value}${node.fields.slug}`,
component: path.resolve(`./src/templates/blog-post.js`),
context: {
slug: node.fields.slug,
languageSet: node.frontmatter.set || '',
languages,
locale: value,
routed: true,
originalPath: node.fields.slug
}
})
})
})
resolve()
})
})
}
exports.onCreatePage = ({ page, actions: { createPage, deletePage } }) => {
// FIXME: 404 language bug?
// see https://github.com/gatsbyjs/gatsby/issues/5129
if (page.path.includes('404')) {
return Promise.resolve()
}
return new Promise(resolve => {
const redirect = path.resolve('src/i18n/redirect.js')
const redirectPage = {
...page,
component: redirect,
context: {
languages,
locale: '', // FIXME: 404 language bug?
routed: false,
redirectPage: page.path
}
}
deletePage(page)
createPage(redirectPage)
languages.forEach(({ value }) =>
createPage({
...page,
originalPath: page.path,
path: `/${value}${page.path}`,
context: {
languages,
locale: value,
routed: true,
originalPath: page.path
}
})
)
resolve()
})
}