-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
113 lines (99 loc) · 2.47 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
const fs = require("fs");
const path = require("path");
// 1. Make sure the pages directory exists
exports.onPreBootstrap = ({ reporter, store }, options) => {
const { program } = store.getState();
const contentPath = options.contentPath || "data";
const imagesPath = options.imagesPath || "images";
const dir = path.join(program.directory, imagesPath);
if (!fs.existsSync(contentPath)) {
reporter.info(`creating the ${contentPath} directory`);
fs.mkdirSync(contentPath);
}
if (!fs.existsSync(dir)) {
reporter.info(`creating the ${dir} directory`);
fs.mkdirSync(dir);
}
};
// 2. Define the page type
exports.sourceNodes = ({ actions }) => {
const types = `
type Link {
name: String!
link: String
}
type Section {
id: String!
header: String
links: [Link!]
}
type StaticPage implements Node @dontInfer {
id: ID!
name: String!
documentLanguage: String!
bio: String!
profilePicAltText: String!
sections: [Section!]!
slug: String!
}
`;
actions.createTypes(types);
};
// 3. Define resolvers for any custom fields (slug)
exports.createResolvers = ({ createResolvers }, options) => {
const basePath = options.basePath || "/";
const slugify = string => {
const slug = string
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/(^-|-%)+/g, "");
return `/${basePath}/${slug}`.replace(/\/\/+/g, "/");
};
createResolvers({
StaticPage: {
slug: {
resolve: source => slugify(source.name)
}
}
});
};
// 4. Query for pages and create them
exports.createPages = async ({ actions, graphql, reporter }, options) => {
const result = await graphql(`
query StaticPageQuery {
allStaticPage {
edges {
node {
id
name
bio
documentLanguage
profilePicAltText
sections {
header
links {
name
link
}
}
slug
}
}
}
}
`);
if (result.errors) {
reporter.panic("error loading static pages", result.errors);
return;
}
const staticPages = result.data.allStaticPage.edges;
staticPages.forEach(staticPage => {
actions.createPage({
path: staticPage.node.slug,
component: require.resolve("./src/templates/aboutMe.js"),
context: {
pageId: staticPage.node.id
}
});
});
};