-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-config.js
323 lines (305 loc) · 9.78 KB
/
gatsby-config.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const activeEnv = process.env.GATSBY_ACTIVE_ENV || process.env.NODE_ENV || 'development';
console.log(`Using environment config: "${activeEnv}"`)
require('dotenv').config({
path: `.env.${activeEnv}`,
});
// Read package.json settings
const { version } = require('./package.json');
// Read environment variables
const PATH_PREFIX = process.env.PATH_PREFIX || '';
const BASE_URL = process.env.BASE_URL || 'https://localhost:9000';
const ANALYTICS_UA = process.env.ANALYTICS_UA || '';
const OFFLINE_PWA = 'true' === process.env.OFFLINE_PWA;
const FACEBOOK_ID = process.env.FACEBOOK_ID || '';
/**
* Converts the entries collection of the provided object into an array of objects with just two entries: 'key' and 'value'.
* For example: {en: 'house', fr: 'maison'} => [{lang: 'en', value: 'house'}, {lang: 'fr', value: 'maison'}]
* This is useful for serializing dictionaries in GraphQL, where objects with variable field names cannot be used in queries.
*/
function objectToObjectArray(obj, valueName = 'value', keyName = 'lang' ) {
return Object.entries(obj).map(([k, v]) => ({ [keyName]: k, [valueName]: v }));
}
// Main metadata settings
const pathPrefix = PATH_PREFIX;
const baseUrl = BASE_URL;
const cardFileName = 'card.jpg';
const themeColor = '#663399';
const themeBackground = '#ffffff';
const shareSites = { twitter: true, facebook: true, telegram: true, whatsapp: true, email: true, pinterest: false, classroom: false, moodle: false };
const shareMeta = { hash: 'chemical,blog', via: 'mr-gatsby' };
const supportedLanguages = ['en', 'ca', 'es'];
const langNames = ['English', 'Català', 'Español'];
const defaultLanguage = 'en';
const localizedTitles = {
ca: 'Plantilla de lloc amb blog',
es: 'Plantilla de sitio con blog',
en: 'Template of site with blog',
};
const title = localizedTitles[defaultLanguage];
const localizedAuthors = {
ca: 'En Gatsby',
es: 'Sr. Gatsby',
en: 'Mr. Gatsby',
};
const author = localizedAuthors[defaultLanguage];
const localizedShortTitles = {
ca: 'Gatsby Blog',
es: 'Gatsby Blog',
en: 'Gatsby Blog',
};
const shortTitle = localizedShortTitles[defaultLanguage];
const localizedDescriptions = {
ca: 'Plantilla de lloc web Gatsby multi-idioma fet amb Material Design',
es: 'Plantilla de sitio web multi-idioma construido con Material Design',
en: 'Gatsby multi-language blog template made with Material Design',
};
const description = localizedDescriptions[defaultLanguage];
const specialPages = ['/search/', '/blog/'];
// Gatsby config options
const config = {
pathPrefix: PATH_PREFIX,
siteMetadata: {
title,
localizedTitles: objectToObjectArray(localizedTitles, 'title'),
shortTitle,
localizedShortTitles: objectToObjectArray(localizedShortTitles, 'shortTitle'),
author,
localizedAuthors: objectToObjectArray(localizedAuthors, 'author'),
description,
localizedDescriptions: objectToObjectArray(localizedDescriptions, 'desc'),
pathPrefix,
baseUrl,
version,
social: {
twitter: 'test',
},
defaultLanguage,
supportedLanguages,
langNames,
specialPages,
shareSites,
shareMeta,
cardFileName,
facebookId: FACEBOOK_ID,
},
plugins: [
// Static pages
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content/static`,
name: 'static',
},
},
// Blog entries
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content/blog`,
name: 'blog',
},
},
// Misc. assets
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content/assets`,
name: 'assets',
},
},
// Transform MarkDown files
{
resolve: 'gatsby-plugin-mdx',
options: {
extensions: ['.mdx', '.md'],
gatsbyRemarkPlugins: [
'gatsby-remark-attr',
'gatsby-remark-autolink-headers',
'gatsby-remark-responsive-iframe',
{
resolve: 'gatsby-remark-images',
options: {
maxWidth: 650,
// maxHeight: 600,
// fit: 'contain',
// background: 'white',
linkImagesToOriginal: true,
showCaptions: ['title'],
markdownCaptions: false,
sizeByPixelDensity: false,
backgroundColor: 'white',
quality: 50,
withWebp: {
quality: 80,
},
tracedSVG: false,
loading: 'lazy',
disableBgImageOnAlpha: false,
disableBgImage: false,
/**
* wrapperStyle: ({aspectRatio, sizes, originalImg, originalName, density, presentationWidth, presentationHeight}) => {
* // Function should return a string with CSS attributes
* return `max-height:300px;`
* },
*/
},
},
{
resolve: 'gatsby-remark-responsive-iframe',
options: {
wrapperStyle: 'margin-bottom: 1.0725rem',
},
},
'gatsby-remark-prismjs',
'gatsby-remark-copy-linked-files',
'gatsby-remark-smartypants',
],
},
},
// Transform images
'gatsby-transformer-sharp',
'gatsby-plugin-sharp',
// Generate manifest file
{
resolve: 'gatsby-plugin-manifest',
options: {
name: title,
short_name: shortTitle,
description: `${description} (${version})`,
start_url: `/`,
background_color: themeBackground,
theme_color: themeColor,
display: 'standalone',
icon: 'content/assets/icons/logo.svg',
lang: defaultLanguage,
localize: supportedLanguages.map(lang => ({
lang,
start_url: `/${lang}/`,
name: localizedTitles[lang],
short_name: localizedShortTitles[lang],
description: localizedDescriptions[lang],
})),
},
},
// Generate social cards
{
resolve: '@francesc/gatsby-plugin-multilang-twitter-cards',
options: {
localizedTitles, // website titles
localizedAuthors, // website author names
defaultLanguage, // default language
// separator: '|', // defaults to '|'
// Free image from: https://pxhere.com/en/photo/1367344
background: require.resolve('./content/assets/base.jpg'), // path to 1200x630px file or hex code, defaults to black (#000000)
fontColor: '#900AA0', // defaults to white (#ffffff)
titleFontSize: 136, // defaults to 96
subtitleFontSize: 48, // defaults to 60
// fontStyle: 'monospace', // defaults to "monospace"
fontFile: require.resolve('./content/assets/oswald-v29-latin-500.ttf'), // will override fontStyle - path to custom TTF font
cardFileName, // defaults to "twitter-card.jpg"
},
},
// Generate feeds
{
resolve: 'gatsby-plugin-feed-mdx',
options: {
// this base query will be merged with any queries in each feed
query: `
{
site {
siteMetadata {
title
description
baseUrl
pathPrefix
}
}
}`,
feeds: supportedLanguages.map(lang => {
return {
query: `{
allMdx(filter: {fields: {lang: {eq: "${lang}"}}}, sort: {fields: frontmatter___date, order: DESC}) {
edges {
node {
fields {
slug
}
body
excerpt
frontmatter {
date
title
description
}
}
}
}
}`,
serialize: ({ query: { site: { siteMetadata: { baseUrl, pathPrefix } }, allMdx } }) => {
return allMdx.edges.map(({ node: { fields: { slug }, frontmatter, excerpt /*, body*/ } }) => {
const url = `${baseUrl}${pathPrefix}/${lang}${slug}`;
return Object.assign(
{},
frontmatter,
{
description: frontmatter.description || excerpt || 'no description provided',
url,
guid: url,
// Provide full content:
// custom_elements: [{ 'content:encoded': body }]
});
});
},
output: lang === defaultLanguage ? 'rss.xml' : `rss-${lang}.xml`,
title: `${localizedTitles[lang]} (${langNames[supportedLanguages.indexOf(lang)]})`,
language: lang,
};
}),
},
},
//
// Fill-in 'head' fields
'gatsby-plugin-react-helmet',
// Material-UI settings
{
resolve: 'gatsby-theme-material-ui',
options: {
webFontsConfig: {
fonts: {
google: [
{ family: 'Roboto', variants: ['300', '400', '500'] },
// { family: 'Open Sans', variants: ['300', '400', '500'] },
],
},
},
},
},
// i18n
{
resolve: 'gatsby-plugin-intl',
options: {
path: `${__dirname}/src/intl`,
languages: supportedLanguages,
defaultLanguage,
// redirect disabled because of erroneus redirections with `static` folder
redirect: true,
},
},
],
};
// Google analytics
if (ANALYTICS_UA) {
config.plugins.push(
{
resolve: 'gatsby-plugin-google-analytics',
options: {
trackingId: ANALYTICS_UA,
},
}
);
}
// Set-up the service worker
if (OFFLINE_PWA)
config.plugins.push('gatsby-plugin-offline');
// Export the resulting object
module.exports = config;