-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.ts
140 lines (118 loc) · 3.47 KB
/
gatsby-node.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type { GatsbyNode, Node, Resolvers } from 'gatsby';
import { createRemoteFileNode } from 'gatsby-source-filesystem';
import { resolve } from 'path';
import rehypeParse from 'rehype-parse';
import rehypeStringify from 'rehype-stringify';
import unified from 'unified';
import { imageProcessor } from './config/plugins';
const gatsbyNode: GatsbyNode = {
async onCreateNode({ node, actions, store, cache, reporter, createNodeId }) {
const { createNode, createNodeField } = actions;
const processImage = async (field: string) => {
if (!node[field]) {
return;
}
const fileNode = await createRemoteFileNode({
url: node[field] as string,
parentNodeId: node.id,
store,
cache,
reporter,
createNode,
createNodeId
});
createNodeField({ node, name: 'imageId', value: fileNode.id });
};
const processHtml = async () => {
if (!node.html) {
return;
}
const file = await unified()
.use(rehypeParse, { fragment: true })
.use(imageProcessor, {
store,
cache,
reporter,
createNode,
createNodeId,
parentId: node.id
})
.use(rehypeStringify)
.process(node.html as string);
createNodeField({ node, name: 'html', value: String(file) });
};
if (node.internal.type === 'GhostPost') {
await processImage('feature_image');
await processHtml();
}
if (node.internal.type === 'GhostAuthor') {
await processImage('profile_image');
}
},
async createSchemaCustomization({ actions }) {
const { createTypes } = actions;
createTypes(`
type GhostPost implements Node {
image: File @link(from: "fields.imageId")
}
type GhostAuthor implements Node {
image: File @link(from: "fields.imageId")
}
`);
},
async createResolvers({ createResolvers }): Promise<void> {
const resolvers: Resolvers = {
GhostTag: {
postCount: {
type: 'Int!',
async resolve(node: Node, _, { nodeModel }): Promise<number> {
const { totalCount } = await nodeModel.findAll({
type: 'GhostPost',
query: {
filter: {
primary_tag: {
slug: {
eq: node.slug
}
}
}
}
});
return totalCount();
}
}
}
};
createResolvers(resolvers);
},
async createPages({ graphql, reporter, actions }) {
const { createPage } = actions;
const createPagesForNode = async (type: string, prefix: string) => {
const { data, errors } = await graphql<{ [key: string]: { nodes: { slug: string }[] } }>(`
{
all${type} {
nodes {
slug
}
}
}
`);
if (errors || !data) {
return reporter.panicOnBuild(`Failed to create pages for ${type}`, errors);
}
data[`all${type}`].nodes.forEach(({ slug }) => {
createPage({
path: `${prefix}${slug}`,
component: resolve(__dirname, `src/templates/${type}.tsx`),
context: {
slug
}
});
});
};
await createPagesForNode('GhostAuthor', '/author/');
await createPagesForNode('GhostPost', '/');
await createPagesForNode('GhostTag', '/tag/');
}
};
export default gatsbyNode;