forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
206 lines (183 loc) · 5.86 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
197
198
199
200
201
202
203
204
205
206
const path = require('path');
const get = require('lodash.get');
const slugify = require('@sindresorhus/slugify');
const visit = require('unist-util-visit');
const rawMDX = require('@mdx-js/mdx');
const matter = require('gray-matter');
const mdastToString = require('mdast-util-to-string');
const createPaginatedPages = require('gatsby-paginate');
const generateUrl = require('./generateUrl');
const compiler = rawMDX.createMdxAstCompiler({ remarkPlugins: [] });
// we're using github-slugger for heading ids so that we're consistent with GitHub & because gatsby-remark-autolink-headers which adds the actual links uses github-slugger
const slugs = require('github-slugger')();
const PROJECT_ROOT = path.resolve('..');
// Used for sorting the navigation:
const GROUPS = [
'',
'quick-start',
'blog',
'tutorials',
'guides',
'API',
'discussions',
'list-plugins',
'road-map',
];
const SUB_GROUPS = [
'',
'apps',
'field-types',
'adapters',
'field-adapters',
'api',
'authentication-strategies',
'utilities',
];
const createDocsPages = async ({ createPage, graphql }) =>
graphql(`
{
allMdx(
sort: {
fields: [fields___sortOrder, fields___sortSubOrder, fields___order, fields___pageTitle]
}
) {
edges {
node {
id
excerpt
fields {
slug
description
navGroup
navSubGroup
workspaceSlug
sortOrder
sortSubOrder
order
author
date
isPackageIndex
isIndex
pageTitle
draft
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors);
}
const pages = result.data.allMdx.edges.filter(page => {
const {
node: {
fields: { draft },
},
} = page;
return Boolean(!draft);
});
let navGroups = {};
pages.forEach(({ node: { id, fields } }) => {
if (fields.navGroup) {
if (!navGroups[fields.navGroup]) {
navGroups[fields.navGroup] = [{ node: { id, fields } }];
} else {
navGroups[fields.navGroup].push({ node: { id, fields } });
}
}
// navGroups.add(fields.navGroup)
createPage({
path: `${fields.slug}`,
component: path.resolve(`src/templates/docs.js`),
context: {
mdPageId: id,
...fields,
isBlog: fields.navGroup === 'blog',
}, // additional data can be passed via context
});
});
Object.entries(navGroups).forEach(([baseSlug, pages]) => {
if (baseSlug !== 'quick-start' && baseSlug !== 'API') {
createPaginatedPages({
edges: pages,
pathPrefix: slugify(baseSlug),
createPage: createPage,
context: { name: baseSlug, showSearch: true },
pageTemplate: 'src/templates/listPage.js',
});
}
});
});
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions;
return createDocsPages({ createPage, graphql });
};
const getEditUrl = absPath =>
`https://github.com/keystonejs/keystone/edit/master/${path.relative(PROJECT_ROOT, absPath)}`;
exports.onCreateNode = async ({ node, actions, getNode }) => {
const { createNodeField } = actions;
// Only for our markdown files
if (get(node, 'internal.type') === `Mdx`) {
// Get the parent node which includes info about the file paths, etc
const parent = getNode(node.parent);
const { sourceInstanceName, relativePath } = parent;
const isPackage = !GROUPS.includes(sourceInstanceName);
let { data, content } = matter(node.rawBody, { delimiters: ['<!--[meta]', '[meta]-->'] });
const navGroup = data.section === 'api' ? 'API' : data.section;
const navSubGroup = data.subSection;
const order = data.order || 99999999999;
let pageTitle = data.title || '';
const ast = compiler.parse(content);
let description;
let heading;
slugs.reset();
let headingIds = [];
visit(ast, node => {
if (!description && node.type === 'paragraph') {
description = mdastToString(node);
}
if (!heading && node.type === 'heading' && node.depth === 1) {
heading = mdastToString(node);
}
if (node.type === 'heading') {
headingIds.push(slugs.slug(mdastToString(node)));
}
});
// This value is added in `gatsby-config` as the "name" of the plugin.
// Since we scan every workspace and add that as a separate plugin, we
// have the opportunity there to add the "name", which we pull from the
// workspace's `package.json`, and can use here.
const fieldsToAdd = {
navGroup: navGroup || null, // Empty string is fine
navSubGroup: navSubGroup || null,
workspaceSlug: slugify(sourceInstanceName),
editUrl: getEditUrl(get(node, 'fileAbsolutePath')),
// The full path to this "node"
slug: data.slug || generateUrl(parent),
sortOrder: GROUPS.indexOf(navGroup) === -1 ? 999999 : GROUPS.indexOf(navGroup),
sortSubOrder:
SUB_GROUPS.indexOf(navSubGroup) === -1 ? 999999 : SUB_GROUPS.indexOf(navSubGroup),
order,
isPackageIndex: isPackage && relativePath === 'README.md',
isIndex: !isPackage && relativePath === 'index.md',
pageTitle: pageTitle,
author: data.author,
date: new Date(data.date).toDateString(),
draft: Boolean(data.draft),
description,
heading,
headingIds,
};
// see: https://github.com/gatsbyjs/gatsby/issues/1634#issuecomment-388899348
Object.keys(fieldsToAdd)
.filter(key => fieldsToAdd[key] !== undefined || fieldsToAdd[key] !== null)
.forEach(key => {
createNodeField({
node,
name: key,
value: fieldsToAdd[key],
});
});
}
};