-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
54 lines (50 loc) · 1.51 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
const path = require("path");
exports.onCreateNode = ({ node, actions }) => {
const { createNode, createNodeField } = actions
if (node.internal.type == "items") {
const slug = slugify(node.title)
createNodeField({
node,
name: `slug`,
value: slug
})
}
}
function slugify(string) {
const a = 'àáäâãåăæçèéëêǵḧìíïîḿńǹñòóöôœøṕŕßśșțùúüûǘẃẍÿź·/_,:;'
const b = 'aaaaaaaaceeeeghiiiimnnnooooooprssstuuuuuwxyz------'
const p = new RegExp(a.split('').join('|'), 'g')
return string.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(p, c => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/&/g, '-and-') // Replace & with ‘and’
.replace(/[^\w\-]+/g, '') // Remove all non-word characters
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(`
{
allItems {
nodes {
fields {
slug
}
}
}
}
`);
return Promise.all(
result.data.allItems.nodes.map(async node => {
await createPage({
path: node.fields.slug,
component: path.resolve("./src/pages/episode.js"),
context: {
slug: node.fields.slug
}
});
})
);
};