This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gatsby-node.js
130 lines (110 loc) · 4.07 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
const path = require(`path`);
const locales = require(`./config/i18n`);
const {
localizedSlug,
findKey,
removeTrailingSlash
} = require(`./src/utils/gatsby-node-helpers`);
const resources = require("./src/i18n/resources.json");
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions;
// First delete the incoming page that was automatically created by Gatsby
// So everything in src/pages/
deletePage(page);
// Grab the keys ('en' & 'de') of locales and map over them
Object.keys(locales).map(lang => {
// Use the values defined in "locales" to construct the path
const localizedPath = locales[lang].default
? page.path
: `${locales[lang].path}${page.path}`;
return createPage({
// Pass on everything from the original page
...page,
// Since page.path returns with a trailing slash (e.g. "/de/")
// We want to remove that
path: removeTrailingSlash(localizedPath),
// Pass in the locale as context to every page
// This context also gets passed to the src/components/layout file
// This should ensure that the locale is available on every page
context: {
...page.context,
locale: lang,
localeResources: resources[lang] ? resources[lang] : {},
dateFormat: locales[lang].dateFormat
}
});
});
};
// As you don't want to manually add the correct languge to the frontmatter of each file
// a new node is created automatically with the filename
// It's necessary to do that -- otherwise you couldn't filter by language
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
// Check for "Mdx" type so that other files (e.g. images) are exluded
if (node.internal.type === `Mdx`) {
// Use path.basename
// https://nodejs.org/api/path.html#path_path_basename_path_ext
const name = path.basename(node.fileAbsolutePath, `.mdx`);
// Check if post.name is "index" -- because that's the file for default language
// (In this case "en")
const isDefault = name === `index`;
// Find the key that has "default: true" set (in this case it returns "en")
const defaultKey = findKey(locales, o => o.default === true);
// Files are defined with "name-with-dashes.lang.mdx"
// name returns "name-with-dashes.lang"
// So grab the lang from that string
// If it's the default language, pass the locale for that
const lang = isDefault ? defaultKey : name.split(`.`)[1];
createNodeField({ node, name: `locale`, value: lang });
createNodeField({ node, name: `isDefault`, value: isDefault });
}
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const postTemplate = require.resolve(`./src/templates/post.js`);
const result = await graphql(`
{
blog: allFile(filter: { sourceInstanceName: { eq: "blog" } }) {
edges {
node {
relativeDirectory
childMdx {
fields {
locale
isDefault
}
frontmatter {
title
}
}
}
}
}
}
`);
if (result.errors) {
console.error(result.errors);
return;
}
const postList = result.data.blog.edges;
postList.forEach(({ node: post }) => {
// All files for a blogpost are stored in a folder
// relativeDirectory is the name of the folder
const slug = post.relativeDirectory;
const title = post.childMdx.frontmatter.title;
// Use the fields created in exports.onCreateNode
const locale = post.childMdx.fields.locale;
const isDefault = post.childMdx.fields.isDefault;
createPage({
path: localizedSlug({ isDefault, locale, slug }),
component: postTemplate,
context: {
// Pass both the "title" and "locale" to find a unique file
// Only the title would not have been sufficient as articles could have the same title
// in different languages, e.g. because an english phrase is also common in german
locale,
title
}
});
});
};