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 0
/
Copy pathgatsby-node.js
122 lines (108 loc) · 2.74 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
const { resolve } = require(`path`)
const slugify = require(`slugify`)
const { compose, groupWith, last, takeLast, head } = require(`ramda`)
const isPage = node =>
/\/content\/pages\/[^_]+\/index\.[^/]+/.test(node.fileAbsolutePath)
const getNLast = n =>
compose(
head,
takeLast(n)
)
const getGroup = node => {
const absolutePath = node.fileAbsolutePath.split(`/`)
const filename = last(absolutePath)
return filename.indexOf(`index`) === 0
? getNLast(3)(absolutePath)
: getNLast(2)(absolutePath)
}
const groupByPage = groupWith((a, b) => getGroup(a) === getGroup(b))
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
createNodeField({
node,
name: `isPage`,
value: isPage(node),
})
if (isPage(node)) {
createNodeField({
node,
name: `slug`,
value: `/${node.frontmatter.lang}/${slugify(node.frontmatter.slug)}`,
})
}
if (node.frontmatter && node.frontmatter.lang) {
createNodeField({
node,
name: `language`,
value: node.frontmatter.lang,
})
}
}
exports.createPages = async ({ actions, getNodes, graphql }) => {
const { createPage, createNodeField } = actions
// get pages and posts
const graphNodes = await graphql(`
{
languages: allLanguagesYaml {
edges {
node {
id
title
}
}
}
pages: allMarkdownRemark(
filter: { fields: { isPage: { eq: true }, slug: { ne: null } } }
) {
edges {
node {
fields {
slug
}
frontmatter {
lang
layout
}
}
}
}
}
`)
if (graphNodes.errors) {
return Promise.reject(graphNodes.errors)
}
// create pages & blog posts
graphNodes.data.pages.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: resolve(`./src/page-templates/${node.frontmatter.layout}.js`),
context: {
slug: node.fields.slug,
lang: node.frontmatter.lang,
},
})
})
const pages = getNodes().filter(isPage)
// console.log(groupByPage(pages))
groupByPage(pages).forEach(group =>
group.forEach((node, index, array) =>
createNodeField({
node,
name: `translations`,
value: array.filter(n => n.id !== node.id).map(node => node.id),
})
)
)
return true
}
exports.onCreateBabelConfig = ({ actions: { setBabelPlugin } }) => {
// setBabelPlugin({ name: `babel-plugin-tailwind` })
setBabelPlugin({ name: `babel-plugin-emotion` })
}
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [resolve(__dirname, `src`), `node_modules`],
},
})
}