-
Notifications
You must be signed in to change notification settings - Fork 11
/
gatsby-node.js
93 lines (87 loc) · 2.24 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
const path = require('path');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
exports.onCreateWebpackConfig = ({ config, actions }) => {
actions.setWebpackConfig({
plugins: [
new ForkTsCheckerWebpackPlugin({
checkSyntacticErrors: true,
formatter: 'codeframe',
tslint: './tslint.json',
watch: './src',
}),
],
resolve: {
alias: {
'@components': path.join(__dirname, './src/components'),
'@lib': path.join(__dirname, './src/lib'),
'@api': path.join(__dirname, './src/api'),
'@utils': path.join(__dirname, './src/utils'),
'@screens': path.join(__dirname, './src/screens'),
'@forms': path.join(__dirname, './src/forms'),
'@theme': path.join(__dirname, './src/theme'),
},
},
});
};
/**
* Slugify a string
* @param s Any string
*/
function toSlug(s) {
if (!s) {
return '';
}
s = s.toLowerCase().trim();
s = s.replace(/ & /g, ' and ');
s = s.replace(/[ ]+/g, '-');
s = s.replace(/[-]+/g, '-');
s = s.replace(/[^a-z0-9-]+/g, '');
return s;
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `Episode`) {
createNodeField({
node,
name: `slug`,
value: `/radio/${node.id}`,
});
}
};
exports.createPages = ({ graphql, actions }) => {
return new Promise((resolve, reject) => {
const episodeTemplate = path.resolve('./src/templates/episode.tsx');
const episodeQuery = /* GraphQL */ `
{
allEpisode(sort: { fields: [date], order: DESC }, limit: 1000) {
edges {
node {
id
title
description
fields {
slug
}
}
}
}
}
`;
resolve(
graphql(episodeQuery).then(result => {
if (result.errors) {
reject(result.errors);
}
result.data.allEpisode.edges.forEach(edge => {
actions.createPage({
path: edge.node.fields.slug,
component: episodeTemplate,
context: {
slug: edge.node.fields.slug,
},
});
});
})
);
});
};