-
Notifications
You must be signed in to change notification settings - Fork 3
/
gatsby-node.js
116 lines (106 loc) · 3.17 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
const fs = require('fs')
const path = require('path')
const Remark = require('remark')
const sectionTitles = {
guide: 'Guide',
community: 'Community'
}
const langsWithPaths = ['es', 'ru'];
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators
Object.keys(sectionTitles).forEach(section => {
createPage({
path: `${section}/search`,
component: path.resolve('src/templates/Search.js'),
context: {
section: section,
sectionTitle: sectionTitles[section],
relatedFiles: `/^${section}\\/.*\\.md$/`
}
})
})
graphql(`{
allFile(filter:{relativePath:{regex:"/^([a-z]{2}\\\\/)?(guide|community)\\\\/.*\\\\.md$/"}}) {
edges {
node {
relativePath
childMarkdownRemark {id}
}
}
}
}`
)
.then(({ errors, data }) => {
if (errors) return Promise.reject(errors)
if (!data.allFile) {
throw new Error('No files found')
}
data.allFile.edges.forEach(
({ node: { relativePath, childMarkdownRemark } }) => {
if (!childMarkdownRemark) return
let targetPath = relativePath.slice(0, -'.md'.length)
if (relativePath.match(/index\.md$/)) {
targetPath = relativePath.slice(0, -'/index.md'.length)
}
const pathSplit = relativePath.split('/')
const [section, langPath] = langsWithPaths.indexOf(pathSplit[0]) === -1
? [pathSplit[0], '']
: [pathSplit[1], pathSplit[0]]
createPage({
path: targetPath,
component: path.resolve('src/templates/Guide.js'),
context: {
section: langPath ? langPath + '/' + section : section,
sectionTitle: sectionTitles[section],
relativePath,
relatedFiles: langPath ? `/^${langPath}\\/${section}\\/.*\\.md$/` : `/^${section}\\/.*\\.md$/`
}
})
}
)
})
.then(() => {
return graphql(
`{
allFile(filter:{relativePath:{regex:"/api.*html$/"}}) {
edges {
node {
relativePath
}
}
}
}`
).then(({ errors, data }) => {
if (errors) return Promise.reject(errors)
if (!data.allFile) {
throw new Error('No files found')
}
data.allFile.edges.forEach(({ node: { relativePath } }) => {
createPage({
path: relativePath,
component: path.resolve('src/templates/API.js'),
context: {
relativePath
}
})
})
})
})
}
exports.onCreateNode = ({ node, boundActionCreators }) => {
const { createPage } = boundActionCreators
if (!node.relativePath || !node.childMarkdownRemark) return
const { relativePath } = node
if (!relativePath.match(/^(es|guide|community)\//)) return
let targetPath = relativePath.slice(0, -'.md'.length)
if (relativePath.match(/index\.md$/)) {
targetPath = relativePath.slice(0, -'/index.md'.length)
}
createPage({
path: targetPath,
component: path.resolve('src/templates/Guide.js'),
context: {
relativePath
}
})
}