-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-node.js
79 lines (69 loc) · 1.64 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
const fs = require("fs");
const path = require("path");
const slugify = require("@sindresorhus/slugify");
const templatesDir = path.resolve(__dirname, "src", "templates");
const pageTemplate = (fileName) => path.resolve(templatesDir, fileName);
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const {
data: {
allGraphCmsPage: { nodes: pages },
},
} = await graphql(
`
fragment assetData on GraphCMS_Asset {
id
url
}
{
allGraphCmsPage {
nodes {
id
slug
seoTitle
seoDescription
seoFocusKeywords
seoImage {
...assetData
}
seoImageAltText
}
}
}
`
);
pages.forEach(async (page) => {
const { id, slug } = page;
const fileName = pageTemplate(`${slug}-template.js`);
const templateExists = await fs.existsSync(fileName);
const component = templateExists
? fileName
: pageTemplate("page-template.js");
createPage({
path: slug === "home" ? `/` : `/${slug}`,
component,
context: {
id,
page,
},
});
});
};
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
GraphCMS_Category: {
slug: {
type: "String",
resolve: ({ title }) => slugify(title, { lower: true }),
},
},
GraphCMS_Tool: {
formattedUrl: {
type: "String",
resolve: ({ url }) =>
url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split("/")[0],
},
},
};
createResolvers(resolvers);
};