gatsby-attainlabs-cms 1.0.36 → 1.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/gatsby-node.js +141 -95
- package/package.json +1 -1
package/gatsby-node.js
CHANGED
|
@@ -57,6 +57,84 @@ const generatePage = async (blocks, layout, isNested) => {
|
|
|
57
57
|
return await prettier.format(pageContent, { parser: "babel-ts" });
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
+
const generateBlogPage = async (blocks, layout, isNested) => {
|
|
61
|
+
// Validate input parameters
|
|
62
|
+
if (!Array.isArray(blocks)) {
|
|
63
|
+
throw new Error("Invalid parameters passed to createPage.");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Helper function to generate Slice components
|
|
67
|
+
const generateSlices = (blocks) => {
|
|
68
|
+
return blocks.map((b) => `<Slice alias="${b.sliceId}" />`).join("\n");
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// Generate the page content
|
|
72
|
+
const pageContent = `
|
|
73
|
+
/* This is a generated file by the gatsby-attainlabs-cms plugin. Any changes will be overwritten on the next build. */
|
|
74
|
+
import { Slice } from "gatsby";
|
|
75
|
+
import SEO from "../../../components/SEO";
|
|
76
|
+
|
|
77
|
+
export const Head = ({ pageContext }: any) => {
|
|
78
|
+
const blocks = pageContext.blocks;
|
|
79
|
+
const meta = blocks.root.props.meta;
|
|
80
|
+
return (
|
|
81
|
+
<SEO
|
|
82
|
+
title={meta ? meta.title : ""}
|
|
83
|
+
description={meta ? meta.description : ""}
|
|
84
|
+
noIndex={true}
|
|
85
|
+
/>
|
|
86
|
+
);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const BlogPage = ({pageContext}:any ) => {
|
|
90
|
+
return (
|
|
91
|
+
<div sx={{bg:"white"}}>
|
|
92
|
+
${generateSlices(blocks)}
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export default BlogPage;
|
|
98
|
+
`;
|
|
99
|
+
|
|
100
|
+
// Format the generated code using Prettier (must await!)
|
|
101
|
+
return await prettier.format(pageContent, { parser: "babel-ts" });
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const getAllBlogs = (data) => {
|
|
105
|
+
const allBlogs = [];
|
|
106
|
+
const getDatePublished = (entry) => {
|
|
107
|
+
const date =
|
|
108
|
+
entry?.draft?.blocks?.root?.props?.datePublished ||
|
|
109
|
+
entry?.blocks?.root?.props?.datePublished;
|
|
110
|
+
return date ? new Date(date).getTime() : 0;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
for (let [key, value] of Object.entries(data)) {
|
|
114
|
+
const category =
|
|
115
|
+
value?.draft?.blocks?.root?.props?.category ||
|
|
116
|
+
value?.blocks?.root?.props?.category;
|
|
117
|
+
if (category === "blog") {
|
|
118
|
+
value = { ...value, parentFolder: value.folderUrl };
|
|
119
|
+
allBlogs.push(value);
|
|
120
|
+
}
|
|
121
|
+
if (value.hasOwnProperty("children")) {
|
|
122
|
+
for (let [childKey, childValue] of Object.entries(value["children"])) {
|
|
123
|
+
const childCategory =
|
|
124
|
+
childValue?.draft?.blocks?.root?.props?.category ||
|
|
125
|
+
childValue?.blocks?.root?.props?.category;
|
|
126
|
+
|
|
127
|
+
if (childCategory === "blog") {
|
|
128
|
+
childValue = { ...childValue, parentFolder: value.folderUrl };
|
|
129
|
+
allBlogs.push(childValue);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return allBlogs.sort((a, b) => getDatePublished(b) - getDatePublished(a));
|
|
136
|
+
};
|
|
137
|
+
|
|
60
138
|
require("dotenv").config();
|
|
61
139
|
// Load PAT from env instead of hardcoding (fallback to old const for now but warn)
|
|
62
140
|
exports.onPreInit = async (_, pluginOptions) => {
|
|
@@ -392,29 +470,8 @@ exports.sourceNodes = async (
|
|
|
392
470
|
|
|
393
471
|
const firebaseData = await fetchBlogData();
|
|
394
472
|
|
|
395
|
-
const allBlogs =
|
|
396
|
-
|
|
397
|
-
for (const [key, value] of Object.entries(firebaseData)) {
|
|
398
|
-
const category =
|
|
399
|
-
value?.draft?.blocks?.root?.props?.category ||
|
|
400
|
-
value?.blocks?.root?.props?.category;
|
|
401
|
-
|
|
402
|
-
if (category === "blog") {
|
|
403
|
-
allBlogs.push(value);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
if (value.hasOwnProperty("children")) {
|
|
407
|
-
for (const [childKey, childValue] of Object.entries(value["children"])) {
|
|
408
|
-
const childCategory =
|
|
409
|
-
childValue?.draft?.blocks?.root?.props?.category ||
|
|
410
|
-
childValue?.blocks?.root?.props?.category;
|
|
473
|
+
const allBlogs = getAllBlogs(firebaseData);
|
|
411
474
|
|
|
412
|
-
if (childCategory === "blog") {
|
|
413
|
-
allBlogs.push(childValue);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
475
|
for (const blog of allBlogs) {
|
|
419
476
|
const id = createNodeId(`Blog-${blog.id}`);
|
|
420
477
|
const node = {
|
|
@@ -456,81 +513,15 @@ exports.createPages = async ({ actions, store }, pluginOptions) => {
|
|
|
456
513
|
const firebaseData = await fetch(
|
|
457
514
|
`https://attain-finance-cms-default-rtdb.firebaseio.com/cms/brands/${brands[brand]}/pages.json`
|
|
458
515
|
);
|
|
516
|
+
const firebaseJson = await firebaseData.json();
|
|
459
517
|
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
allBlogs {
|
|
463
|
-
edges {
|
|
464
|
-
node {
|
|
465
|
-
blocks {
|
|
466
|
-
content {
|
|
467
|
-
props {
|
|
468
|
-
content {
|
|
469
|
-
props {
|
|
470
|
-
text
|
|
471
|
-
image {
|
|
472
|
-
desktop {
|
|
473
|
-
url
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
# type
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
root {
|
|
482
|
-
props {
|
|
483
|
-
author
|
|
484
|
-
datePublished
|
|
485
|
-
pageUrl
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
`);
|
|
494
|
-
const postsPerPage = 5;
|
|
495
|
-
const blogs = blogData.allBlogs.edges;
|
|
496
|
-
const numPages = Math.ceil(blogs.length / postsPerPage);
|
|
497
|
-
|
|
498
|
-
Array.from({ length: numPages }).forEach((_, i) => {
|
|
499
|
-
createPage({
|
|
500
|
-
path: i === 0 ? `/blogs/` : `/blogs/${i + 1}/`,
|
|
501
|
-
component: path.resolve(
|
|
502
|
-
path.join(siteRoot, "src/cms/pages", "index.tsx")
|
|
503
|
-
),
|
|
504
|
-
// path.join(siteRoot, "src/cms/pages", `${folderPath}.tsx`);
|
|
505
|
-
context: {
|
|
506
|
-
limit: postsPerPage,
|
|
507
|
-
skip: i * postsPerPage,
|
|
508
|
-
numPages,
|
|
509
|
-
currentPage: i + 1,
|
|
510
|
-
},
|
|
511
|
-
});
|
|
512
|
-
});
|
|
513
|
-
|
|
514
|
-
// Create individual blog pages
|
|
515
|
-
// let count = 0;
|
|
516
|
-
// let page = 1;
|
|
517
|
-
// blogs.forEach(({ node }: any) => {
|
|
518
|
-
// count++;
|
|
519
|
-
// if (count > postsPerPage) {
|
|
520
|
-
// page++;
|
|
521
|
-
// count = 1;
|
|
522
|
-
// }
|
|
523
|
-
// createPage({
|
|
524
|
-
// path: `/blogs/${page > 1 ? `${page}/` : ""}${node.slug}/`,
|
|
525
|
-
// component: path.resolve("./src/templates/blogPage.tsx"),
|
|
526
|
-
// context: {
|
|
527
|
-
// ...node,
|
|
528
|
-
// page,
|
|
529
|
-
// },
|
|
530
|
-
// });
|
|
531
|
-
// });
|
|
518
|
+
const firebaseBlogs = getAllBlogs(firebaseJson);
|
|
519
|
+
const blogsPerFolder = 5;
|
|
532
520
|
|
|
533
|
-
const
|
|
521
|
+
const blogsWithFolderNumbers = firebaseBlogs.map((blog, idx) => {
|
|
522
|
+
const folderNumber = Math.floor(idx / blogsPerFolder) + 1;
|
|
523
|
+
return { ...blog, folderNumber };
|
|
524
|
+
});
|
|
534
525
|
|
|
535
526
|
async function processPage(page, parentPath = "") {
|
|
536
527
|
// Check for folderUrl first — handle nested folders immediately
|
|
@@ -608,7 +599,62 @@ exports.createPages = async ({ actions, store }, pluginOptions) => {
|
|
|
608
599
|
}
|
|
609
600
|
}
|
|
610
601
|
|
|
602
|
+
// async function processBlogs(page, parentPath = "") {
|
|
603
|
+
// if (page.template === "visual-editor" && page.published && page.type === "blog") {
|
|
604
|
+
// const {
|
|
605
|
+
// blocks: {
|
|
606
|
+
// content,
|
|
607
|
+
// root: {
|
|
608
|
+
// props: { pageUrl, layout },
|
|
609
|
+
// },
|
|
610
|
+
// },
|
|
611
|
+
// id,
|
|
612
|
+
// folderNumber,
|
|
613
|
+
// parentFolder
|
|
614
|
+
// } = page;
|
|
615
|
+
|
|
616
|
+
// // Create slice for each block
|
|
617
|
+
// await Promise.all(
|
|
618
|
+
// content.map(async (b) => {
|
|
619
|
+
// const name = b.props.component.name;
|
|
620
|
+
// const sliceId = `block--${pageUrl}--${name}--${id}--${crypto.randomUUID()}`;
|
|
621
|
+
|
|
622
|
+
// createSlice({
|
|
623
|
+
// id: sliceId,
|
|
624
|
+
// component: path.resolve(
|
|
625
|
+
// siteRoot,
|
|
626
|
+
// "src/cms/components/sliceWrapper.tsx"
|
|
627
|
+
// ),
|
|
628
|
+
// context: {
|
|
629
|
+
// componentPath: `./src/cms/components${b.props.component.path}/index.tsx`,
|
|
630
|
+
// ...b.props,
|
|
631
|
+
// },
|
|
632
|
+
// });
|
|
633
|
+
|
|
634
|
+
// b["sliceId"] = sliceId;
|
|
635
|
+
// return b;
|
|
636
|
+
// })
|
|
637
|
+
// );
|
|
638
|
+
|
|
639
|
+
// // Generate page file
|
|
640
|
+
// const pageSource = await generateBlogPage(content, layout, parentPath);
|
|
641
|
+
// const folderPath = pageUrl;
|
|
642
|
+
// const outPath = path.join(siteRoot, "src/cms/pages", `${parentFolder}/${folderNumber}/${folderPath}.tsx`);
|
|
643
|
+
|
|
644
|
+
// await fse.outputFile(outPath, pageSource);
|
|
645
|
+
|
|
646
|
+
// await createPage({
|
|
647
|
+
// path: `${parentFolder}/${folderNumber}/${folderPath}`,
|
|
648
|
+
// component: outPath,
|
|
649
|
+
// context: { ...page },
|
|
650
|
+
// });
|
|
651
|
+
// }
|
|
652
|
+
// }
|
|
653
|
+
|
|
611
654
|
for (const page of Object.values(firebaseJson)) {
|
|
612
655
|
await processPage(page);
|
|
613
656
|
}
|
|
657
|
+
// for (const blog of blogsWithFolderNumbers) {
|
|
658
|
+
// await processBlogs(blog, brand === "LendDirect" ? "resources" : "blogs");
|
|
659
|
+
// }
|
|
614
660
|
};
|