This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
createPages.js
133 lines (123 loc) · 3.96 KB
/
createPages.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
const { resolve } = require(`path`)
const {
// isHomePage,
isPage,
isPost,
notIsErrorPage,
prepareSortedTuple,
sortEnglishLast,
} = require(`./helpers`)
module.exports = store => ({ actions, getNodes }) => {
const { redirects } = store
const { createPage, createNodeField } = actions
// setup mappings
const allNodes = getNodes()
const Languages = allNodes.filter(x => x.internal.type === `LanguagesAml`)
// const PagesAndPosts = allNodes.filter(R.either(isPage, isPost))
const PagesAndPosts = allNodes.filter(node => isPage(node) || isPost(node))
prepareSortedTuple(PagesAndPosts).forEach(group =>
sortEnglishLast(group).forEach((node, _, array) => {
// if (node.frontmatter.slug === `welcome-to-gaiamas-blog`) console.log(node)
const { lang, url, slug } = node.fields
const { layout, shortId, shortlink, oldId, oldSlug } = node.frontmatter
const page = {
component: resolve(`./src/templates/${layout}.js`),
path: url,
context: { url, slug, lang },
}
// https://github.com/gatsbyjs/gatsby/issues/5129#issuecomment-442397391
// https://github.com/davidbailey00/manuelbieh.de/blob/62714633fb13b6e04c9e24f587114419299af946/gatsby-node.js#L66
if (slug === `404`) {
// create root 404
if (lang === `en`) {
createPage({
...page,
path: `/404`,
})
}
page.matchPath = `/${lang}/*`
}
createPage(page)
createNodeField({
node,
name: `translations`,
value: array
.map(n => {
const lang = Languages.find(
x => x.frontmatter.id === n.frontmatter.lang
)
if (!lang) return null
return Object.assign({}, lang.frontmatter, {
to: n.fields.url,
fields: {
url: n.fields.url,
},
frontmatter: {
title: n.frontmatter.title,
lang: n.frontmatter.lang,
slug: n.frontmatter.slug,
cover: {
publicURL: n.frontmatter.cover
? n.frontmatter.cover.publicURL
: ``,
},
},
})
})
.sort((a, b) =>
a.frontmatter.lang < b.frontmatter.lang
? -1
: a.frontmatter.lang > b.frontmatter.lang
? 1
: 0
),
})
// set up short url redirects
// const slugShort = isHomePage(node) ? `/` : `/${index}`
// createNodeField({
// node,
// name: `slug_short`,
// value: notIsErrorPage(node) ? slugShort : null,
// })
if (notIsErrorPage(node)) {
// redirects.push(
// [
// slugShort,
// url + (isHomePage(node)
// ? ``
// : `?ref=${encodeURIComponent(slugShort)}`),
// `301!`,
// node.frontmatter.lang === `de` && `Language=de`,
// ]
// .filter(Boolean)
// .join(` `)
// .trim()
// )
// &fb_locale=en_US redirections as in https://stackoverflow.com/a/20827962/3484824
node.fields.translations
.filter(x => x.id !== node.frontmatter.lang)
.forEach(lang =>
redirects.push(
[
`${node.fields.url}?fb_locale=${lang.lc}`,
`${lang.to}?ref=${encodeURIComponent(
`${node.fields.url}?fb_locale=${lang.lc}`
)}`,
`301!`,
`Language=${lang.id}`,
]
.filter(x => x)
.join(` `)
.trim()
)
)
}
// legacy short url redirects
const idsToRedirect = [shortId, shortlink, oldId, oldSlug]
idsToRedirect.map(id => {
if (!id) return false
return redirects.push(`${`/${lang}`}/${id} ${url} 301!`)
})
})
)
}