This repository has been archived by the owner on Jul 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
213 lines (188 loc) · 5.94 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
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
/* eslint-disable no-console */
const path = require(`path`);
const R = require('ramda');
const { haveSameItem, getPreviousNextNode, kebabCase } = require('./src/utils/helpers');
const getBaseUrl = require('./src/utils/getBaseUrl');
const {
site: { lang: defaultLang = 'en', displayTranslations },
supportedLanguages,
} = require('./config');
// group by language
const byLangKey = R.groupBy(R.path(['node', 'fields', 'langKey']));
// group by directoryName
const byDirectoryName = R.groupBy(R.path(['node', 'fields', 'directoryName']));
const translationsByDirectory = posts => {
const gpDirPosts = byDirectoryName(posts);
const dirNames = R.keys(gpDirPosts);
const otherLangs = R.compose(
R.map(R.map(R.path(['node', 'fields', 'langKey']))),
R.values,
)(gpDirPosts);
return R.zipObj(dirNames, otherLangs);
};
/**
* @param {*func} createPage
*/
function PageMaker(createPage) {
const blogIndex = path.resolve('./src/templates/blog-index.js');
const blogPost = path.resolve('./src/templates/blog-post.js');
const tagsTotal = path.resolve('./src/templates/tags.js');
const tagPage = path.resolve('./src/templates/tag-page.js');
return {
createBlogIndex() {
// Create index pages for all supported languages
Object.keys(supportedLanguages).forEach(langKey => {
const slug = getBaseUrl(defaultLang, langKey);
createPage({
path: slug,
component: blogIndex,
context: {
langKey,
slug,
},
});
});
},
createBlogPost(posts) {
const translationsInfo = displayTranslations ? translationsByDirectory(posts) : [];
posts.forEach(post => {
// posts in same language
const postLangKey = post.node.fields.langKey;
const postsInSameLang = posts.filter(({ node }) => postLangKey === node.fields.langKey);
const indexInSameLang = postsInSameLang.findIndex(
p => p.node.fields.slug === post.node.fields.slug,
);
const { previous, next } = getPreviousNextNode(postsInSameLang, indexInSameLang);
// posts in same tags
const postTags = post.node.pageAttributes.tags;
const postsInSameTag = posts.filter(({ node }) =>
haveSameItem(postTags, node.pageAttributes.tags),
);
const indexInSameTag = postsInSameTag.findIndex(
p => p.node.fields.slug === post.node.fields.slug,
);
const { previous: previousInSameTag, next: nextInSameTag } = getPreviousNextNode(
postsInSameTag,
indexInSameTag,
);
// translations
let translationsLink = [];
if (displayTranslations && R.path(['node', 'fields', 'directoryName'], post)) {
const dirName = post.node.fields.directoryName;
const translations = R.without([postLangKey], translationsInfo[dirName]);
translationsLink = translations.map(trans => ({
name: supportedLanguages[trans],
url: `/${trans}/${dirName}/`.replace(`/${defaultLang}`, ''),
langKey: trans,
}));
}
createPage({
path: post.node.fields.slug,
component: blogPost,
context: {
slug: post.node.fields.slug,
previous,
next,
previousInSameTag,
nextInSameTag,
translationsLink,
},
});
});
},
createTagIndex(postsGroupByLang) {
Object.keys(postsGroupByLang).forEach(langKey => {
// Make tags-total
createPage({
path: `${getBaseUrl(defaultLang, langKey)}tags/`,
component: tagsTotal,
context: {
langKey,
},
});
});
},
createTagPage(postsGroupByLang) {
Object.keys(postsGroupByLang).forEach(langKey => {
// Tag pages:
let tags = [];
postsGroupByLang[langKey].forEach(post => {
if (R.path(['node', 'pageAttributes', 'tags'], post)) {
tags = tags.concat(post.node.pageAttributes.tags);
}
});
// Eliminate duplicate tags
tags = R.uniq(tags);
// Make tag pages
tags.forEach(tag => {
const slug = `${getBaseUrl(defaultLang, langKey)}tags/${kebabCase(tag)}/`;
createPage({
path: slug,
component: tagPage,
context: {
tag,
langKey,
slug,
},
});
});
});
},
};
}
exports.createPages = ({ graphql, actions: { createPage } }) => {
const pageMaker = PageMaker(createPage);
return new Promise((resolve, reject) => {
pageMaker.createBlogIndex();
resolve(
graphql(
`
{
allAsciidoc(sort: { fields: [revision___date], order: DESC }, limit: 1000) {
edges {
node {
fields {
slug
langKey
directoryName
}
document {
title
}
revision {
date
}
pageAttributes {
tags
}
}
}
}
}
`,
).then(result => {
if (result.errors) {
console.log(result.errors);
reject(result.errors);
}
const posts = result.data.allAsciidoc.edges;
const gpLangPosts = byLangKey(posts);
pageMaker.createBlogPost(posts);
pageMaker.createTagIndex(gpLangPosts);
pageMaker.createTagPage(gpLangPosts);
return null;
}),
);
});
};
exports.onCreateNode = ({ node, actions }) => {
if (R.path(['internal', 'type'], node) === 'Asciidoc') {
actions.createNodeField({
node,
name: 'directoryName',
value: node.paths.from.source.file.slice(
0, node.paths.from.source.file.lastIndexOf(`/`)
)
});
}
};