Skip to content

Commit b6e4110

Browse files
refactor(node-api): modularize gatsby-node page creators
Signed-off-by: Manishnemade12 <mnemade140@gmail.com>
1 parent 8d494bd commit b6e4110

File tree

4 files changed

+215
-146
lines changed

4 files changed

+215
-146
lines changed

gatsby-node.js

Lines changed: 26 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const {
1515
getExcludedCollections,
1616
isFullSiteBuild,
1717
} = require("./src/utils/build-collections");
18+
const createBlogPages = require("./src/node-api/createBlogPages");
19+
const createKanvasLabPages = require("./src/node-api/createKanvasLabPages");
20+
const createSistentComponentPages = require("./src/node-api/createSistentComponentPages");
1821

1922
const shouldBuildFullSite = isFullSiteBuild();
2023
const excludedCollections = new Set(
@@ -107,23 +110,31 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
107110

108111
const HandbookTemplate = path.resolve("src/templates/handbook-template.js");
109112

113+
await createBlogPages({
114+
graphql,
115+
createPage: envCreatePage,
116+
reporter,
117+
isCollectionEnabled,
118+
blogPostTemplate,
119+
blogCategoryListTemplate,
120+
blogTagListTemplate,
121+
});
122+
123+
await createKanvasLabPages({
124+
graphql,
125+
createPage: envCreatePage,
126+
reporter,
127+
labTemplate: LabTemplate,
128+
});
129+
130+
await createSistentComponentPages({
131+
graphql,
132+
createPage,
133+
reporter,
134+
});
135+
110136
const res = await graphql(`
111137
{
112-
blogPosts: allMdx(
113-
filter: {
114-
fields: { collection: { eq: "blog" } }
115-
frontmatter: { published: { eq: true } }
116-
}
117-
) {
118-
nodes {
119-
fields {
120-
slug
121-
}
122-
internal {
123-
contentFilePath
124-
}
125-
}
126-
}
127138
resourcePosts: allMdx(
128139
filter: {
129140
fields: { collection: { eq: "resources" } }
@@ -234,32 +245,6 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
234245
}
235246
}
236247
}
237-
blogTags: allMdx(
238-
filter: {
239-
fields: { collection: { eq: "blog" } }
240-
frontmatter: { published: { eq: true } }
241-
}
242-
) {
243-
group(field: { frontmatter: { tags: SELECT } }) {
244-
nodes {
245-
id
246-
}
247-
fieldValue
248-
}
249-
}
250-
blogCategory: allMdx(
251-
filter: {
252-
fields: { collection: { eq: "blog" } }
253-
frontmatter: { published: { eq: true } }
254-
}
255-
) {
256-
group(field: { frontmatter: { category: SELECT } }) {
257-
nodes {
258-
id
259-
}
260-
fieldValue
261-
}
262-
}
263248
${
264249
shouldBuildFullSite
265250
? `memberPosts: allMdx(
@@ -327,19 +312,6 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
327312
}
328313
}
329314
}
330-
labs: allMdx(
331-
filter: { fields: { collection: { eq: "kanvas-labs" } } }
332-
) {
333-
nodes {
334-
fields {
335-
slug
336-
collection
337-
}
338-
internal {
339-
contentFilePath
340-
}
341-
}
342-
}
343315
learncontent: allMdx(
344316
filter: { fields: { collection: { eq: "content-learn" } } }
345317
) {
@@ -358,25 +330,6 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
358330
}
359331
}
360332
}
361-
sistentComponents: allMdx(
362-
filter: {
363-
fields: { collection: { eq: "sistent" } }
364-
}
365-
) {
366-
group(field: { fields: { componentName: SELECT } }) {
367-
fieldValue
368-
nodes {
369-
fields {
370-
slug
371-
componentName
372-
pageType
373-
}
374-
internal {
375-
contentFilePath
376-
}
377-
}
378-
}
379-
}
380333
}
381334
`);
382335

@@ -386,7 +339,6 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
386339
return;
387340
}
388341

389-
const blogs = res.data.blogPosts.nodes;
390342
const resources = res.data.resourcePosts.nodes;
391343
const news = res.data.newsPosts.nodes;
392344

@@ -415,7 +367,6 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
415367
const handbook = res.data.handbookPages.nodes;
416368

417369
const singleWorkshop = res.data.singleWorkshop.nodes;
418-
const labs = res.data.labs.nodes;
419370

420371
if (isCollectionEnabled("events") && events.length > 0) {
421372
paginate({
@@ -427,44 +378,6 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
427378
});
428379
}
429380

430-
if (isCollectionEnabled("blog")) {
431-
blogs.forEach((blog) => {
432-
envCreatePage({
433-
path: blog.fields.slug,
434-
component: `${blogPostTemplate}?__contentFilePath=${blog.internal.contentFilePath}`,
435-
context: {
436-
slug: blog.fields.slug,
437-
},
438-
});
439-
});
440-
}
441-
442-
const blogCategory = res.data.blogCategory.group;
443-
if (isCollectionEnabled("blog")) {
444-
blogCategory.forEach((category) => {
445-
envCreatePage({
446-
path: `/blog/category/${slugify(category.fieldValue)}`,
447-
component: blogCategoryListTemplate,
448-
context: {
449-
category: category.fieldValue,
450-
},
451-
});
452-
});
453-
}
454-
455-
const BlogTags = res.data.blogTags.group;
456-
if (isCollectionEnabled("blog")) {
457-
BlogTags.forEach((tag) => {
458-
envCreatePage({
459-
path: `/blog/tag/${slugify(tag.fieldValue)}`,
460-
component: blogTagListTemplate,
461-
context: {
462-
tag: tag.fieldValue,
463-
},
464-
});
465-
});
466-
}
467-
468381
if (isCollectionEnabled("resources")) {
469382
resources.forEach((resource) => {
470383
envCreatePage({
@@ -567,16 +480,6 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
567480
});
568481
});
569482

570-
labs.forEach((lab) => {
571-
envCreatePage({
572-
path: lab.fields.slug,
573-
component: `${LabTemplate}?__contentFilePath=${lab.internal.contentFilePath}`,
574-
context: {
575-
slug: lab.fields.slug,
576-
},
577-
});
578-
});
579-
580483
if (shouldBuildFullSite) {
581484
integrations.forEach((integration) => {
582485
envCreatePage({
@@ -761,29 +664,6 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
761664
}
762665
});
763666

764-
// Create Sistent component pages dynamically from MDX
765-
// Use grouping to identify which sub-pages (Tabs) exist for each component
766-
const sistentGroups = res.data.sistentComponents.group;
767-
const sistentTemplate = path.resolve("src/templates/sistent-component.js");
768-
769-
sistentGroups.forEach((group) => {
770-
const componentName = group.fieldValue;
771-
// content-learn uses different fields, sistent uses componentName.
772-
773-
const availablePages = group.nodes.map((node) => node.fields.pageType);
774-
775-
group.nodes.forEach((node) => {
776-
createPage({
777-
path: node.fields.slug,
778-
component: `${sistentTemplate}?__contentFilePath=${node.internal.contentFilePath}`,
779-
context: {
780-
slug: node.fields.slug,
781-
componentName: componentName,
782-
availablePages: availablePages,
783-
},
784-
});
785-
});
786-
});
787667
};
788668

789669
// slug starts and ends with '/' so parts[0] and parts[-1] will be empty

src/node-api/createBlogPages.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const slugify = require("../utils/slugify");
2+
3+
const createBlogPages = async ({
4+
graphql,
5+
createPage,
6+
reporter,
7+
isCollectionEnabled,
8+
blogPostTemplate,
9+
blogCategoryListTemplate,
10+
blogTagListTemplate,
11+
}) => {
12+
const result = await graphql(`
13+
{
14+
blogPosts: allMdx(
15+
filter: {
16+
fields: { collection: { eq: "blog" } }
17+
frontmatter: { published: { eq: true } }
18+
}
19+
) {
20+
nodes {
21+
fields {
22+
slug
23+
}
24+
internal {
25+
contentFilePath
26+
}
27+
}
28+
}
29+
blogTags: allMdx(
30+
filter: {
31+
fields: { collection: { eq: "blog" } }
32+
frontmatter: { published: { eq: true } }
33+
}
34+
) {
35+
group(field: { frontmatter: { tags: SELECT } }) {
36+
fieldValue
37+
}
38+
}
39+
blogCategory: allMdx(
40+
filter: {
41+
fields: { collection: { eq: "blog" } }
42+
frontmatter: { published: { eq: true } }
43+
}
44+
) {
45+
group(field: { frontmatter: { category: SELECT } }) {
46+
fieldValue
47+
}
48+
}
49+
}
50+
`);
51+
52+
if (result.errors) {
53+
reporter.panicOnBuild("Error while running GraphQL query for blog pages.");
54+
return;
55+
}
56+
57+
if (!isCollectionEnabled("blog")) {
58+
return;
59+
}
60+
61+
const blogs = result.data.blogPosts.nodes;
62+
const blogCategory = result.data.blogCategory.group;
63+
const blogTags = result.data.blogTags.group;
64+
65+
blogs.forEach((blog) => {
66+
createPage({
67+
path: blog.fields.slug,
68+
component: `${blogPostTemplate}?__contentFilePath=${blog.internal.contentFilePath}`,
69+
context: {
70+
slug: blog.fields.slug,
71+
},
72+
});
73+
});
74+
75+
blogCategory.forEach((category) => {
76+
createPage({
77+
path: `/blog/category/${slugify(category.fieldValue)}`,
78+
component: blogCategoryListTemplate,
79+
context: {
80+
category: category.fieldValue,
81+
},
82+
});
83+
});
84+
85+
blogTags.forEach((tag) => {
86+
createPage({
87+
path: `/blog/tag/${slugify(tag.fieldValue)}`,
88+
component: blogTagListTemplate,
89+
context: {
90+
tag: tag.fieldValue,
91+
},
92+
});
93+
});
94+
};
95+
96+
module.exports = createBlogPages;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const createKanvasLabPages = async ({ graphql, createPage, reporter, labTemplate }) => {
2+
const result = await graphql(`
3+
{
4+
labs: allMdx(
5+
filter: { fields: { collection: { eq: "kanvas-labs" } } }
6+
) {
7+
nodes {
8+
fields {
9+
slug
10+
}
11+
internal {
12+
contentFilePath
13+
}
14+
}
15+
}
16+
}
17+
`);
18+
19+
if (result.errors) {
20+
reporter.panicOnBuild("Error while running GraphQL query for Kanvas labs pages.");
21+
return;
22+
}
23+
24+
const labs = result.data.labs.nodes;
25+
26+
labs.forEach((lab) => {
27+
createPage({
28+
path: lab.fields.slug,
29+
component: `${labTemplate}?__contentFilePath=${lab.internal.contentFilePath}`,
30+
context: {
31+
slug: lab.fields.slug,
32+
},
33+
});
34+
});
35+
};
36+
37+
module.exports = createKanvasLabPages;

0 commit comments

Comments
 (0)