forked from GDG-Nantes/Devfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
116 lines (110 loc) · 2.81 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 locales = require("./locales/config.json").map((l) => l.code);
const path = require("path");
/** Removes the useless pages (ex: /en/team.fr/)
* Changes path for files with extensions: /fr/team.fr/ -> /fr/team/
* Duplicates the french pages to create entrypoints without explicit languages: https://github.com/gatsbyjs/themes/issues/124
*/
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions;
// /en/team.fr/ -> prefix: en; suffix: en
const extensionMatch =
/\/(?:(?<prefix>[a-z]{2})\/)?[^.]*(?:\.(?<suffix>[a-z]{2}))?\//.exec(
page.path
);
if (extensionMatch) {
let pathWithoutSuffix = page.path;
if (
extensionMatch.groups.suffix &&
(extensionMatch.groups.prefix === extensionMatch.groups.suffix ||
(!extensionMatch.groups.prefix &&
extensionMatch.groups.suffix === "fr"))
) {
pathWithoutSuffix = page.path
.replace(`.${extensionMatch.groups.suffix}/`, "/")
.replace("/index/", "/");
createPage({
...page,
path: pathWithoutSuffix,
});
}
if (extensionMatch.groups.suffix) {
deletePage(page);
}
}
};
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
// Query for markdown nodes to use in creating pages.
const { data, errors } = await graphql(
`
{
allSessionsYaml {
edges {
node {
key
slot
speakers
tags
talkType
title
room
language
complexity
abstract
}
}
}
allSpeakersYaml {
edges {
node {
key
name
feature
city
company
companyLogo
photoUrl
bio
socials {
twitter
github
}
}
}
}
}
`
);
if (errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
// Sessions
const sessionPageTemplate = path.resolve(
"src/components/session/sessionPageTemplate.tsx"
);
data.allSessionsYaml.edges.forEach(({ node: session }) => {
const path = "sessions/" + session.key;
createPage({
path,
component: sessionPageTemplate,
context: {
session,
},
});
});
// Speakers
const speakerPageTemplate = path.resolve(
"src/components/speaker/speakerPageTemplate.tsx"
);
data.allSpeakersYaml.edges.forEach(({ node: speaker }) => {
const path = "speakers/" + speaker.key;
createPage({
path,
component: speakerPageTemplate,
context: {
speaker,
},
});
});
};