-
Notifications
You must be signed in to change notification settings - Fork 10
/
gatsby-node.ts
187 lines (176 loc) · 4.15 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { GatsbyNode } from "gatsby"
import path from "path"
import { createFilePath } from "gatsby-source-filesystem"
import MarkdownIt from "markdown-it"
import { proposalResolver, svgResolver } from "./gatsby/resolvers"
export const createPages: GatsbyNode["createPages"] = async ({
actions,
graphql,
reporter,
}) => {
const { createPage } = actions
// Filter only for md files in the `src/content/pages` directory. Any md files
// found outside of that should not have a page created for it.
const result = await graphql(`
{
allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/content/(pages)/" } }
) {
edges {
node {
id
fields {
slug
}
frontmatter {
seoTitle
seoDescription
template
}
}
}
}
}
`)
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
// @ts-ignore
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const templateName = node.frontmatter.template || `default`
const template = path.resolve(
path.join(`src/templates/${templateName}`, "index.tsx")
)
createPage({
path: node.fields.slug,
component: template,
context: {
id: node.id,
seo: {
title: node.frontmatter.seoTitle,
description: node.frontmatter.seoDescription,
},
}, // additional data can be passed via context
})
})
}
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] =
({ actions, reporter }) => {
const { createTypes, createFieldExtension } = actions
createFieldExtension({
name: "md",
args: {
from: {
type: "String!",
defaultValue: "",
},
},
extend() {
return {
args: {
from: "String",
},
resolve: async (source: any, args: { from: string }) => {
const fieldValue = source[args.from]
try {
const md = new MarkdownIt()
return md.render(fieldValue)
} catch (error) {
reporter.warn(
`Could not render content from the "${args.from}" field... Using original value`
)
return fieldValue
}
},
}
},
})
const typeDefs = `
type MarkdownRemark implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
harnessThePower: Subitems
tacoRole: Subitems
}
type ProposalContent {
html: String
raw: String
}
type Proposal {
id: String
title: String
createdAt: String
createdBy: String
content: ProposalContent
url: String
}
type Subitems {
subitems: [Item]
}
type Item {
title: String
description: String @md(from: "description")
}
type SVGAttributes {
key: String
value: String
}
type SVG {
name: String,
type: String,
value: String,
attributes: [SVGAttributes]
children: [SVG]
}
`
createTypes(typeDefs)
}
export const onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}
export const onCreateBabelConfig: GatsbyNode["onCreateBabelConfig"] = ({
actions,
}) => {
actions.setBabelPlugin({
name: "@babel/plugin-transform-react-jsx",
options: {
runtime: "automatic",
},
})
}
export const createResolvers: GatsbyNode["createResolvers"] = ({
createResolvers,
reporter,
}) => {
createResolvers({
Query: {
allProposals: {
type: "[Proposal]",
args: {
limit: {
type: "Int",
default: 3,
},
},
resolve: proposalResolver(reporter),
},
},
File: {
svg: {
type: "SVG",
resolve: svgResolver(reporter),
},
},
})
}