forked from prisma/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
178 lines (170 loc) Β· 5.23 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
const path = require(`path`)
exports.onCreateNode = ({ node, getNode, actions }) => {
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', '')}`,
})
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect } = actions
const getTitle = (frontmatter, lang, db) => {
let pageSeoTitle = frontmatter.metaTitle || frontmatter.title
if (lang || db) {
const queryParam = `${lang ? `${lang}${db ? '-' : ''}` : ''}${db ? `${db}` : ''}`
const titleEntry = frontmatter.techMetaTitles
? frontmatter.techMetaTitles.find((item) => item.name === queryParam)
: null
pageSeoTitle = titleEntry ? titleEntry.value : pageSeoTitle
}
return pageSeoTitle
}
const getDesc = (frontmatter, lang, db) => {
let pageSeoDesc = frontmatter.metaDescription || frontmatter.title
if (lang || db) {
const queryParam = `${lang ? `${lang}${db ? '-' : ''}` : ''}${db ? `${db}` : ''}`
const descEntry = frontmatter.techMetaDescriptions
? frontmatter.techMetaDescriptions.find((item) => item.name === queryParam)
: null
pageSeoDesc = descEntry ? descEntry.value : pageSeoDesc
}
return pageSeoDesc
}
return new Promise((resolve, reject) => {
graphql(`
{
allMdx {
edges {
node {
fields {
slug
id
modSlug
}
frontmatter {
title
metaTitle
metaDescription
langSwitcher
dbSwitcher
techMetaTitles {
name
value
}
techMetaDescriptions {
name
value
}
}
body
parent {
... on File {
relativePath
}
}
}
}
}
site {
siteMetadata {
redirects {
from
to
}
}
}
}
`).then((result) => {
result.data.allMdx.edges.forEach(({ node }) => {
const { langSwitcher, dbSwitcher } = node.frontmatter
if (langSwitcher && dbSwitcher) {
langSwitcher.forEach((lang) =>
dbSwitcher.forEach((db) => {
createPage({
path: `${node.fields.modSlug.replace(/\d{2,}-/g, '')}-${lang}-${db}`,
component: path.resolve(`./src/layouts/articleLayout.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) =>
createPage({
path: `${node.fields.modSlug.replace(/\d{2,}-/g, '')}-${lang}`,
component: path.resolve(`./src/layouts/articleLayout.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) =>
createPage({
path: `${node.fields.modSlug.replace(/\d{2,}-/g, '')}-${db}`,
component: path.resolve(`./src/layouts/articleLayout.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/layouts/articleLayout.tsx`),
context: {
id: node.fields.id,
seoTitle: getTitle(node.frontmatter),
seoDescription: getDesc(node.frontmatter),
},
})
}
})
resolve()
const redirects = result.data.site.siteMetadata.redirects
redirects.map((redirect) =>
createRedirect({
fromPath: redirect.from,
toPath: redirect.to,
statusCode: 301,
})
)
})
})
}
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
alias: {
$components: path.resolve(__dirname, 'src/components'),
buble: '@philpl/buble', // to reduce bundle size
},
},
})
}