-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
122 lines (108 loc) · 2.84 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 { createFilePath } = require('gatsby-source-filesystem');
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === `Mdx`) {
const value = createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value,
});
}
};
function slugify(str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = 'àáäâèéëêìíïîòóöôùúüûñç·/_,:;';
var to = 'aaaaeeeeiiiioooouuuunc------';
for (var i = 0, l = from.length; i < l; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str
.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
}
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const result = await graphql(`
query CategoriesAndTags {
tags: allMdx(
filter: {
frontmatter: {
templateKey: { eq: "article" }
published: { eq: true }
}
}
limit: 1000
) {
edges {
node {
frontmatter {
tags
}
}
}
}
categories: allMdx(
filter: {
frontmatter: {
templateKey: { eq: "article" }
published: { eq: true }
}
}
limit: 1000
) {
edges {
node {
frontmatter {
category
}
}
}
}
}
`);
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
// Create Category pages
const categoryTemplate = require.resolve(
`./src/templates/categoryTemplate.js`
);
const categories = result.data.categories.edges
.map(({ node }) => {
return node.frontmatter.category;
})
.filter((t, i, arr) => i + 1 === arr.length || t !== arr[i + 1]);
categories.forEach((category) => {
createPage({
path: `articulos/categoria/${slugify(category)}`,
component: categoryTemplate,
context: {
category: category,
},
});
});
// Create Tag pages
const tagTemplate = require.resolve(`./src/templates/tagTemplate.js`);
const tags = result.data.tags.edges
.map(({ node }) => Object.assign({}, node.frontmatter))
.reduce((acc, e) => acc.concat(e.tags), [])
.map((tag) => tag)
.sort()
.filter((tag, i, tags) => i + 1 === tags.length || tag !== tags[i + 1]);
tags.forEach((tag) => {
createPage({
path: `articulos/tag/${slugify(tag)}`,
component: tagTemplate,
context: {
tag: tag,
},
});
});
};