-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgatsby-node.js
101 lines (97 loc) · 2.63 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
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === "build-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /weixin-js-sdk/,
use: loaders.null(),
},
],
},
})
}
if (stage === 'build-javascript') {
// Turn off source maps
actions.setWebpackConfig({
devtool: false,
})
}
}
// This part comes from gatsby-theme-blog-core/gatsby-node.js
const mdxResolverPassthrough = fieldName => async (
source,
args,
context,
info
) => {
const type = info.schema.getType(`Mdx`)
const mdxNode = context.nodeModel.getNodeById({
id: source.parent,
})
const resolver = type.getFields()[fieldName].resolve
const result = await resolver(mdxNode, args, context, {
fieldName,
})
return result
}
exports.createSchemaCustomization = ({actions, schema}) => {
const {createTypes} = actions
createTypes(`interface BlogPost @nodeInterface {
id: ID!
title: String!
body: String!
slug: String!
date: Date! @dateformat
tags: [String]!
keywords: [String]!
excerpt: String!
cover: String!
categories: [String]!
}`) // adding heroImage as an optional File field to BlogPost
createTypes(
schema.buildObjectType({
name: `MdxBlogPost`,
fields: {
id: {type: `ID!`},
title: {
type: `String!`,
},
slug: {
type: `String!`,
},
date: {type: `Date!`, extensions: {dateformat: {}}},
tags: {type: `[String]!`},
categories: {type: `[String]!`},
keywords: {type: `[String]!`},
excerpt: {
type: `String!`,
args: {
pruneLength: {
type: `Int`,
defaultValue: 140,
},
},
resolve: mdxResolverPassthrough(`excerpt`),
},
cover: {
type: `String!` // adding heroImage as an optional File field to MdxBlogPost
},
body: {
type: `String!`,
resolve: mdxResolverPassthrough(`body`),
},
},
interfaces: [`Node`, `BlogPost`],
})
)
}
exports.onCreateNode = async (
{node, getNode}
) => {
if (node.internal.type === "MdxBlogPost") {
const parentNode = getNode(node.parent)
node.cover = parentNode.frontmatter.cover || ''
node.categories = parentNode.frontmatter.categories || []
}
}