forked from prisma/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.ts
227 lines (214 loc) Β· 6.48 KB
/
gatsby-node.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const path = require('path')
// const docTemplate = path.resolve(`./src/templates/docs.tsx`);
exports.onCreateNode = ({ node, getNode, actions }: any) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
const parent = getNode(node.parent)
let value = parent.relativePath.replace(parent.ext, '')
if (value === 'index') {
value = ''
}
createNodeField({
node,
name: `slug`,
value: `/${value}`,
})
createNodeField({
node,
name: 'id',
value: node.id,
})
createNodeField({
node,
name: 'modSlug',
value: `/${value.replace('/index', '')}`,
})
}
}
const getTitle = (frontmatter: any, lang?: any, db?: any) => {
let pageSeoTitle = frontmatter.metaTitle || frontmatter.title
if (lang || db) {
const queryParam = `${lang ? `${lang}${db ? '-' : ''}` : ''}${db ? `${db}` : ''}`
const titleEntry = frontmatter.techMetaTitles
? frontmatter.techMetaTitles.find((item: any) => item.name === queryParam)
: null
pageSeoTitle = titleEntry ? titleEntry.value : `${pageSeoTitle} : ${queryParam}`
}
return pageSeoTitle
}
const getDesc = (frontmatter: any, lang?: any, db?: any) => {
let pageSeoDesc = frontmatter.metaDescription || frontmatter.title
if (lang || db) {
const queryParam = `${lang ? `${lang}${db ? '-' : ''}` : ''}${db ? `${db}` : ''}`
const descEntry = frontmatter.techMetaDescriptions
? frontmatter.techMetaDescriptions.find((item: any) => item.name === queryParam)
: null
pageSeoDesc = descEntry ? descEntry.value : pageSeoDesc
}
return pageSeoDesc
}
exports.createPages = async ({ graphql, actions, reporter }: any) => {
const { createPage, createRedirect } = actions
const result = await graphql(`
query {
allMdx {
nodes {
id
fields {
slug
id
modSlug
}
frontmatter {
title
metaTitle
metaDescription
langSwitcher
dbSwitcher
techMetaTitles {
name
value
}
techMetaDescriptions {
name
value
}
}
internal {
contentFilePath
}
}
}
site {
siteMetadata {
redirects {
from
to
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild('Error loading MDX result', result.errors)
}
// Create blog post pages.
const posts = result.data.allMdx.nodes
const redirects = result.data.site.siteMetadata.redirects
// you'll call `createPage` for each result
posts.forEach((node: any) => {
// createPage({
// // As mentioned above you could also query something else like frontmatter.title above and use a helper function
// // like slugify to create a slug
// path: node.fields.modSlug
// ? node.fields.modSlug.replace(/\d{2,}-/g, "")
// : "/",
// // Provide the path to the MDX content file so webpack can pick it up and transform it into JSX
// component: `${docTemplate}?__contentFilePath=${node.internal.contentFilePath}`,
// // You can use the values in this context in
// // our page layout component
// context: {
// id: node.id,
// seoTitle: getTitle(node.frontmatter),
// seoDescription: getDesc(node.frontmatter),
// },
// });
const { langSwitcher, dbSwitcher } = node.frontmatter
if (langSwitcher && dbSwitcher) {
langSwitcher.forEach((lang: any) =>
dbSwitcher.forEach((db: any) => {
createPage({
path: `${node.fields.modSlug.replace(/\d{2,}-/g, '')}-${lang}-${db}`,
component: path.resolve('./src/templates/docs.tsx'),
context: {
id: node.fields.id,
seoTitle: getTitle(node.frontmatter, lang, db),
seoDescription: getDesc(node.frontmatter, lang, db),
},
})
})
)
} else if (langSwitcher && !dbSwitcher) {
langSwitcher.forEach((lang: any) =>
createPage({
path: `${node.fields.modSlug.replace(/\d{2,}-/g, '')}-${lang}`,
component: path.resolve('./src/templates/docs.tsx'),
context: {
id: node.fields.id,
seoTitle: getTitle(node.frontmatter, lang, null),
seoDescription: getDesc(node.frontmatter, lang, null),
},
})
)
} else if (!langSwitcher && dbSwitcher) {
node.frontmatter.dbSwitcher.forEach((db: any) =>
createPage({
path: `${node.fields.modSlug.replace(/\d{2,}-/g, '')}-${db}`,
component: path.resolve('./src/templates/docs.tsx'),
context: {
id: node.fields.id,
seoTitle: getTitle(node.frontmatter, null, db),
seoDescription: getDesc(node.frontmatter, null, db),
},
})
)
} else if (!langSwitcher && !dbSwitcher) {
createPage({
path: node.fields.modSlug ? node.fields.modSlug.replace(/\d{2,}-/g, '') : '/',
component: path.resolve('./src/templates/docs.tsx'),
context: {
id: node.fields.id,
seoTitle: getTitle(node.frontmatter),
seoDescription: getDesc(node.frontmatter),
},
})
}
})
redirects
.filter((redirect: any) => !redirect.from.includes('#'))
.map((redirect: any) =>
createRedirect({
fromPath: redirect.from,
toPath: redirect.to,
statusCode: 301,
})
)
}
exports.createSchemaCustomization = ({ actions }: any) => {
const { createTypes } = actions
const typeDefs = `
type MarkdownRemark implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
title: String!
navTitle: String
metaTitle: String
metaDescription: String
langSwitcher: [String!]
dbSwitcher: [String!]
staticLink: Boolean
duration: String
preview: Boolean
deprecated: Boolean
earlyaccess: Boolean
toc: Boolean
hidePage: Boolean
tocDepth: Int
codeStyle: Boolean
}
`
createTypes(typeDefs)
}
exports.onCreateWebpackConfig = ({ stage, actions }: any) => {
actions.setWebpackConfig({
devtool: false,
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
alias: {
$components: path.resolve(__dirname, 'src/components'),
buble: '@philpl/buble', // to reduce bundle size
},
},
})
}