-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node-copy-exmp.js
218 lines (204 loc) · 6.78 KB
/
gatsby-node-copy-exmp.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
214
215
216
217
218
const _ = require("lodash");
const path = require("path");
const { createFilePath } = require("gatsby-source-filesystem");
const createPaginatedPages = require("gatsby-paginate");
const { fmImagesToRelative } = require("gatsby-remark-relative-images");
exports.createPages = ({ actions, graphql, arg }) => {
const { createPage } = actions;
return graphql(`
{
allMarkdownRemark(
limit: 1000
sort: { order: ASC, fields: [frontmatter___order] }
) {
edges {
node {
excerpt(pruneLength: 60)
id
fields {
slug
}
frontmatter {
title
templateKey
hotProductsSelect
tags
categories
cover {
childImageSharp {
fluid(maxWidth: 233, maxHeight: 116, quality: 90) {
src
srcSet
sizes
base64
aspectRatio
}
}
}
version {
power
price
}
vat
date(formatString: "DD.MM.YYYY")
}
}
}
}
}
`).then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()));
return Promise.reject(result.errors);
}
const posts = result.data.allMarkdownRemark.edges;
const blogPosts = result.data.allMarkdownRemark.edges.filter(
edge =>
edge.node.frontmatter.templateKey === "article-page" ||
edge.node.frontmatter.templateKey === "article-page2"
);
createPaginatedPages({
edges: blogPosts,
createPage: createPage,
pageTemplate: "src/templates/blog.js",
pageLength: 6, // This is optional and defaults to 10 if not used
pathPrefix: "blog", // This is optional and defaults to an empty string if not used
context: {}, // This is optional and defaults to an empty object if not used
});
const products = result.data.allMarkdownRemark.edges.filter(
edge => edge.node.frontmatter.templateKey === "product-page"
);
createPaginatedPages({
edges: products,
createPage: createPage,
pageTemplate: "src/templates/products.js",
pageLength: 20, // This is optional and defaults to 10 if not used
pathPrefix: "produkty", // This is optional and defaults to an empty string if not used
context: {
lastProducts: products.slice(0, 4),
hotProducts: products
.filter(
product => product.node.frontmatter.hotProductsSelect === "tak"
)
.slice(0, 4),
}, // This is optional and defaults to an empty object if not used
});
const productsByCategories = result.data.allMarkdownRemark.edges.filter(
edge =>
edge.node.frontmatter.templateKey === "product-page" &&
edge.node.frontmatter.categories === "split"
);
createPaginatedPages({
edges: productsByCategories,
createPage: createPage,
pageTemplate: "src/templates/categories.js",
pageLength: 20, // This is optional and defaults to 10 if not used
pathPrefix: "produkty/kategoria/split", // This is optional and defaults to an empty string if not used
context: {
lastProducts: products.slice(0, 4),
hotProducts: products
.filter(
product => product.node.frontmatter.hotProductsSelect === "tak"
)
.slice(0, 4),
test: arg,
}, // This is optional and defaults to an empty object if not used
});
const multiSplitproducts = result.data.allMarkdownRemark.edges.filter(
edge =>
edge.node.frontmatter.templateKey === "product-page" &&
edge.node.frontmatter.categories === "multi-split"
);
createPaginatedPages({
edges: multiSplitproducts,
createPage: createPage,
pageTemplate: "src/templates/categories.js",
pageLength: 20, // This is optional and defaults to 10 if not used
pathPrefix: "produkty/kategoria/multi-split", // This is optional and defaults to an empty string if not used
context: {
lastProducts: products.slice(0, 4),
hotProducts: products
.filter(
product => product.node.frontmatter.hotProductsSelect === "tak"
)
.slice(0, 4),
test: arg,
}, // This is optional and defaults to an empty object if not used
});
posts.forEach(edge => {
// if (edge.node.frontmatter.templateKey === "products-page") {
// return;
// }
const id = edge.node.id;
createPage({
path: edge.node.fields.slug,
tags: edge.node.frontmatter.tags,
component: path.resolve(
`src/templates/${String(edge.node.frontmatter.templateKey)}.js`
),
// additional data can be passed via context
context: {
id,
hotProducts: products
.filter(
product => product.node.frontmatter.hotProductsSelect === "tak"
)
.slice(0, 4),
lastProducts: products.slice(0, 4),
productCategories: products
.filter(product => product.node.frontmatter.categories)
.map((product, index) => product.node.frontmatter.categories)
.reduce((a, b) => {
if (a.indexOf(b) < 0) a.push(b);
return a;
}, []),
poductsTitleList: products.map(
product => product.node.frontmatter.title
),
},
});
});
// const productsPageContent = result.data.allMarkdownRemark.edges.filter(
// edge => edge.node.frontmatter.templateKey === "products-page"
// );
// productsPageContent.forEach(edge => {
// createPage({
// path: "/products",
// component: path.resolve(`src/templates/products-page.js`),
// });
// });
// Tag pages:
let tags = [];
// Iterate through each post, putting all found tags into `tags`
posts.forEach(edge => {
if (_.get(edge, `node.frontmatter.tags`)) {
tags = tags.concat(edge.node.frontmatter.tags);
}
});
// Eliminate duplicate tags
tags = _.uniq(tags);
// Make tag pages
tags.forEach(tag => {
const tagPath = `/tags/${_.kebabCase(tag)}/`;
createPage({
path: tagPath,
component: path.resolve(`src/templates/tags.js`),
context: {
tag,
},
});
});
});
};
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
fmImagesToRelative(node);
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value,
});
}
};