-
Notifications
You must be signed in to change notification settings - Fork 23
/
gatsby-node.js
184 lines (159 loc) Β· 5.54 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
179
180
181
182
183
184
const path = require("path");
const meta = require("./meta.json");
const buildFindSiblingsQuery = (currentChapterNumber, currentChapterLang) => {
let siblingChapterNumbers = ``;
if (currentChapterNumber == 0) {
siblingChapterNumbers = `[${currentChapterNumber + 1}]`;
} else if (currentChapterNumber == meta.chapters.length) {
siblingChapterNumbers = `[${currentChapterNumber - 1}]`;
} else {
siblingChapterNumbers = `[${currentChapterNumber - 1}, ${
currentChapterNumber + 1
}]`;
}
let languages = ``;
if (currentChapterLang !== "en") {
languages = currentChapterLang;
}
languages = `["${languages}", "en"]`;
return `
query MDXSibling {
allMdx(sort: {fields: fields___chapterNumber, order: ASC}, filter: {fields: {chapterNumber: {in: ${siblingChapterNumbers}}, lang: {in: ${languages}}}}) {
nodes {
fields {
slug
chapterNumber
}
parent {
... on File {
lang: relativeDirectory
}
}
}
}
}
`;
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
const getSiblingPages = (currentChapterNumber, currentChapterLang) => {
/*
This finds the siblings chapters (previous and next chapters) of a given one based on its number and language.
If a sibling cannot be found in the chapter's language, the English one is used as a fallback.
*/
const findSiblingsQuery = buildFindSiblingsQuery(
currentChapterNumber,
currentChapterLang
);
return new Promise(async (resolve, reject) => {
resolve(
graphql(findSiblingsQuery).then((result) => {
if (result.errors) {
console.error(result.errors);
reject(result.errors);
}
let siblings = {
previous: undefined,
next: undefined,
};
result.data.allMdx.nodes.forEach((node) => {
if (node.fields.chapterNumber < currentChapterNumber) {
siblingKind = "previous";
} else if (
node.fields.chapterNumber > currentChapterNumber
) {
siblingKind = "next";
}
if (
siblings[siblingKind] === undefined ||
siblings[siblingKind].parent.lang === null
) {
siblings[siblingKind] = node;
}
});
return siblings;
})
);
});
};
return new Promise(async (resolve, reject) => {
resolve(
graphql(query).then((result) => {
if (result.errors) {
console.error(result.errors);
reject(result.errors);
}
result.data.allMdx.edges.forEach(async ({ node }) => {
const { previous, next } = await getSiblingPages(
node.fields.chapterNumber,
node.parent.lang
);
createPage({
path: node.fields.slug || "/",
component: path.resolve("./src/layouts/chapter.js"),
context: {
id: node.id,
lang: node.parent.lang,
next,
previous,
},
});
});
})
);
});
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === "Mdx") {
const parent = getNode(node.parent);
const kebabTitle = parent.relativePath
.replace(parent.ext, "")
.replace(`${parent.relativeDirectory}/`, "");
const chapterNumber = meta.chapters.indexOf(kebabTitle);
const lang = parent.relativeDirectory;
if (chapterNumber >= 0) {
createNodeField({
node,
name: "chapterNumber",
value: chapterNumber,
});
}
createNodeField({
node,
name: "slug",
value: `/${lang}/chapters/${kebabTitle}`,
});
createNodeField({
node,
name: "id",
value: node.id,
});
createNodeField({
node,
name: "lang",
value: lang,
});
}
};
const query = `
query MDXPages {
allMdx(sort: { fields: fields___chapterNumber }) {
edges {
node {
frontmatter { title }
id
fields {
slug
chapterNumber
}
parent {
... on File {
lang: relativeDirectory
}
}
}
}
}
}
`;