-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
executable file
·101 lines (94 loc) · 2.26 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
import path from 'path';
import { writeJsonData } from './utils/writeJsonData';
export const createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
const PageTemplate = path.resolve('./src/templates/page.tsx');
const TagTemplate = path.resolve('./src/templates/tag.tsx');
const result = await graphql(`
{
allWordpressPost {
nodes {
link
source
title
wordpress_id
date(formatString: "MMM DD, YYYY")
tags {
id
name
slug
}
}
totalCount
}
allWordpressPage {
nodes {
slug
wordpress_id
}
}
allWordpressTag(filter: { count: { gt: 0 } }) {
nodes {
count
wordpress_id
slug
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild('Error while running GraphQL query.');
return;
}
// Create static pages for pages
const Pages = result.data.allWordpressPage.nodes;
Pages.forEach(post => {
createPage({
path: `/${post.slug}`,
component: PageTemplate,
context: {
id: post.wordpress_id,
},
});
});
// Create static pages for tags
const Tags = result.data.allWordpressTag.nodes;
Tags.forEach(async tag => {
createPage({
path: `/tag/${tag.slug}`,
component: TagTemplate,
context: {
id: tag.wordpress_id,
},
});
const postsByTag = await graphql(`
{
allWordpressPost(filter: { tags: { elemMatch: { wordpress_id: { eq: ${tag.wordpress_id} } } } }) {
nodes {
link
source
title
wordpress_id
date(formatString: "MMM DD, YYYY")
tags {
id
name
slug
}
}
totalCount
}
}
`);
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query for tag ${tag.name}.`);
return;
}
writeJsonData({
data: postsByTag.data.allWordpressPost,
rootDir: __dirname,
paths: ['tag', tag.slug],
});
});
writeJsonData({ data: result.data.allWordpressPost, rootDir: __dirname, paths: ['index'] });
};