forked from ZJUSCT/mirror-front
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
196 lines (177 loc) · 5.11 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
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
188
189
190
191
192
193
194
195
196
const { match, compile } = require("path-to-regexp");
const { createFilePath } = require(`gatsby-source-filesystem`)
const { createContentDigest } = require(`gatsby-core-utils`)
const config = require('./config');
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, info);
return result;
}
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions;
// interface for all kinds of documents
createTypes(`interface Document implements Node {
id: ID!
title: String
source: String!
body: String!
html: String!
slug: String!
date: Date @dateformat
tags: [String]!
tableOfContents: JSON
frontmatter: JSON
locale: String!
}`);
// MDX document implements Document interface
createTypes(
schema.buildObjectType({
name: `MdxDocument`,
fields: {
id: { type: `ID!` },
title: {
type: `String`,
},
source: {
type: `String!`,
},
slug: {
type: `String!`,
},
date: { type: `Date`, extensions: { dateformat: {} } },
tags: { type: `[String]!` },
tableOfContents: {
type: `JSON`,
resolve: mdxResolverPassthrough(`tableOfContents`),
},
body: {
type: `String!`,
resolve: mdxResolverPassthrough(`body`),
},
html: {
type: `String!`,
resolve: mdxResolverPassthrough(`html`),
},
frontmatter: {
type: `JSON`,
resolve: mdxResolverPassthrough(`frontmatter`),
},
locale: {
type: `String!`,
},
},
interfaces: [`Node`, `Document`],
extensions: {
infer: false,
},
})
);
}
// create fields for documents slugs and sources
exports.onCreateNode = async (
{ node, actions, getNode, createNodeId, store, cache, reporter },
) => {
const { createNode, createParentChildLink } = actions;
if (node.internal.type !== `Mdx`) return;
// create source field according to source name passed to gatsby-source-filesystem
const fileNode = getNode(node.parent);
const source = fileNode.sourceInstanceName;
const docConfig = config.documentSources[source];
if (!docConfig) reporter.panic(`Unknown source of documents ${source}`);
const { folder: folderLayout, path: pathTemplate } = docConfig;
let slug, locale;
if (node.frontmatter.slug) {
// a relative slug gets turned into a sub path
slug = node.frontmatter.slug;
locale = match(pathTemplate)(path)?.params?.lang;
} else {
// otherwise use the filepath function from gatsby-source-filesystem
const path = createFilePath({
node: fileNode,
getNode,
});
const pathMatch = match(folderLayout)(path);
if (!pathMatch) reporter.panic(`Path ${path} does not match the layout ${folderLayout}`);
const params = pathMatch.params;
locale = params.lang;
// remove default language from slug
if (params.lang === config.defaultLanguage) params.lang = undefined;
slug = compile(pathTemplate)(params);
}
// normalize use of trailing slash
slug = slug.replace(/\/*$/, `/`);
const fieldData = {
title: node.frontmatter.title,
tags: node.frontmatter.tags || [],
slug,
source,
locale,
date: node.frontmatter.date
};
const id = createNodeId(`${node.id} >>> MdxDocument`);
await createNode({
...fieldData,
// required fields
id,
parent: node.id,
children: [],
internal: {
type: `MdxDocument`,
contentDigest: createContentDigest(fieldData),
contentFilePath: node.internal.contentFilePath,
content: JSON.stringify(fieldData),
description: `Mdx implementation of the Document interface`,
},
});
createParentChildLink({ parent: node, child: getNode(id) });
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
const result = await graphql(`
{
allDocument {
nodes {
id
slug
source
frontmatter
internal {
contentFilePath
}
}
}
}
`);
if (result.errors) {
reporter.panic(result.errors);
}
const { allDocument } = result.data;
const docs = allDocument.nodes;
// create pages for documents
docs.forEach((doc, index) => {
const previous = index === docs.length - 1 ? null : docs[index + 1];
const next = index === 0 ? null : docs[index - 1];
const { slug, frontmatter } = doc;
createPage({
path: slug,
component: `${require.resolve(
config.documentSources[doc.source]?.template
)}?__contentFilePath=${doc.internal.contentFilePath}`,
context: {
id: doc.id,
previousId: previous ? previous.id : undefined,
nextId: next ? next.id : undefined,
frontmatter
},
});
})
}