-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgatsby-node.ts
199 lines (176 loc) · 7 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
import { HttpClient, NodeFetcher } from "@miracledevs/paradigm-web-fetch";
import { CreatePagesArgs, CreateNodeArgs, CreatePageArgs, CreateSchemaCustomizationArgs } from "gatsby";
import path from "path";
import { parseContent, query as dynamicPageQuery } from "./src/utils/dynamic-page";
import { getAssetURL } from "./src/utils/get-asset-url";
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`
});
const httpClient = new HttpClient();
var nodeFetcher = new NodeFetcher();
httpClient.setFetcher(nodeFetcher);
const locales = process.env.LOCALES!.split(",");
const defaultLocale = process.env.GATSBY_DEFAULT_LOCALE;
exports.createSchemaCustomization = ({ actions }: CreateSchemaCustomizationArgs) => {
const { createTypes, createFieldExtension } = actions;
createTypes(`
type SitePage implements Node {
locale: String
}
`);
};
exports.onCreateWebpackConfig = ({ stage, actions, getConfig }: any) => {
actions.setWebpackConfig({
resolve: {
modules: ["node_modules", path.resolve(__dirname, "src")],
alias: {
"basic-info": path.resolve(__dirname, "src/app/routes/basic-info"),
"situation-analysis": path.resolve(__dirname, "src/app/routes/situation-analysis"),
"select-drivers": path.resolve(__dirname, "src/app/routes/select-drivers"),
"define-vision": path.resolve(__dirname, "src/app/routes/define-vision"),
"rate-drivers": path.resolve(__dirname, "src/app/routes/rate-drivers"),
"affinity-groups": path.resolve(__dirname, "src/app/routes/affinity-groups"),
"define-objectives": path.resolve(__dirname, "src/app/routes/define-objectives"),
"create-roadmap": path.resolve(__dirname, "src/app/routes/create-roadmap")
}
},
devtool: "eval-source-map"
});
if (stage === "build-javascript" || stage === "develop") {
const config = getConfig();
const miniCssExtractPlugin = config.plugins.find((plugin: any) => plugin.constructor.name === "MiniCssExtractPlugin");
if (miniCssExtractPlugin) miniCssExtractPlugin.options.ignoreOrder = true;
actions.replaceWebpackConfig(config);
}
};
exports.createPages = async ({ actions, graphql }: CreatePagesArgs) => {
const { data } = await graphql<Queries.Query>(
`
query gn {
directus {
not_found {
translations {
button {
link_button_id {
inverted
text
type
url
open_in_new_tab
}
}
id
title
seo {
title
description
}
languages_code {
code
}
}
}
}
}
`
);
const notFoundPage = data!.directus.not_found;
if (notFoundPage && notFoundPage.translations) {
for (const translation of notFoundPage.translations) {
if (
translation &&
translation.languages_code &&
locales.find((x) => x.toLocaleLowerCase() === translation!.languages_code!.code.toLocaleLowerCase())
) {
const fullPath = buildPathWithLanguage(translation.languages_code.code, `/404`);
actions.createPage({
path: fullPath,
matchPath: buildPathWithLanguage(translation.languages_code.code, `/*`),
component: path.resolve(`src/templates/NotFound/index.tsx`),
context: {
page: translation,
...buildDefaultContext(translation.languages_code.code)
}
});
}
}
}
const homePage = data!.directus.home;
if (homePage && homePage.translations) {
for (const translation of homePage.translations) {
if (
translation &&
translation.languages_code &&
translation.languages_code.code != defaultLocale &&
locales.find((x) => x.toLocaleLowerCase() === translation!.languages_code!.code.toLocaleLowerCase())
) {
const fullPath = buildPathWithLanguage(translation.languages_code.code, `/`);
actions.createPage({
path: fullPath,
component: path.resolve(`src/templates/Home/index.tsx`),
context: {
page: translation,
...buildDefaultContext(translation.languages_code.code)
}
});
}
}
}
};
exports.onCreatePage = ({ page, actions }: CreatePageArgs) => {
const { createPage, deletePage } = actions;
// -- Clone page
const newPage = { ...page };
// -- Only for the home
// -- It is the only page created automatically
if (page.path === "/") {
deletePage(page);
newPage.context = {
...buildDefaultContext(defaultLocale ?? "")
};
createPage(newPage);
}
};
const buildPathWithLanguage = (language: string, path: string) => {
return language.toLocaleLowerCase() === defaultLocale?.toLocaleLowerCase() ? path : `/${language.toLocaleLowerCase()}${path}`;
};
const buildDefaultContext = (locale: string) => {
return {
locale: locale.toLocaleLowerCase(),
defaultLocale: defaultLocale,
locales: locales
};
};
exports.onCreateNode = async ({ node, actions }: CreateNodeArgs) => {
const { createNodeField } = actions;
if (node.internal.type === "File" && node.internal.mediaType === "image/svg+xml" && node.parent) {
const svg = await getSvg(node.parent);
createNodeField({
name: "svg",
node,
value: svg
});
}
if (node.internal.type === "SitePage" && node.context) {
const context = node.context as any;
// -- This values are necessary for the search control
node.internal.description = context.title;
node.internal.content = context.content;
node.locale = context.locale;
// -- Add title field for algolia search
createNodeField({
name: "title",
node,
value: context.title
});
}
};
const getSvg = async (id: string) => {
const response = await httpClient.get(getAssetURL(id));
const text = await response.text();
return text;
};
const removeHtml = (text: string) => {
const regexForStripHTML = /<\/?[^>]+(>|$)/g;
return text.replaceAll(regexForStripHTML, "");
};