gatsby-attainlabs-cms 1.0.36 → 1.0.38

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.
Files changed (2) hide show
  1. package/gatsby-node.js +143 -95
  2. package/package.json +1 -1
package/gatsby-node.js CHANGED
@@ -38,6 +38,7 @@ const generatePage = async (blocks, layout, isNested) => {
38
38
  title={meta ? meta.title : ""}
39
39
  description={meta ? meta.description : ""}
40
40
  noIndex={true}
41
+ data={blocks}
41
42
  />
42
43
  );
43
44
  };
@@ -51,12 +52,91 @@ const generatePage = async (blocks, layout, isNested) => {
51
52
  };
52
53
 
53
54
  export default Page;
55
+
54
56
  `;
55
57
 
56
58
  // Format the generated code using Prettier (must await!)
57
59
  return await prettier.format(pageContent, { parser: "babel-ts" });
58
60
  };
59
61
 
62
+ const generateBlogPage = async (blocks, layout, isNested) => {
63
+ // Validate input parameters
64
+ if (!Array.isArray(blocks)) {
65
+ throw new Error("Invalid parameters passed to createPage.");
66
+ }
67
+
68
+ // Helper function to generate Slice components
69
+ const generateSlices = (blocks) => {
70
+ return blocks.map((b) => `<Slice alias="${b.sliceId}" />`).join("\n");
71
+ };
72
+
73
+ // Generate the page content
74
+ const pageContent = `
75
+ /* This is a generated file by the gatsby-attainlabs-cms plugin. Any changes will be overwritten on the next build. */
76
+ import { Slice } from "gatsby";
77
+ import SEO from "../../../components/SEO";
78
+
79
+ export const Head = ({ pageContext }: any) => {
80
+ const blocks = pageContext.blocks;
81
+ const meta = blocks.root.props.meta;
82
+ return (
83
+ <SEO
84
+ title={meta ? meta.title : ""}
85
+ description={meta ? meta.description : ""}
86
+ noIndex={true}
87
+ />
88
+ );
89
+ };
90
+
91
+ const BlogPage = ({pageContext}:any ) => {
92
+ return (
93
+ <div sx={{bg:"white"}}>
94
+ ${generateSlices(blocks)}
95
+ </div>
96
+ );
97
+ };
98
+
99
+ export default BlogPage;
100
+ `;
101
+
102
+ // Format the generated code using Prettier (must await!)
103
+ return await prettier.format(pageContent, { parser: "babel-ts" });
104
+ };
105
+
106
+ const getAllBlogs = (data) => {
107
+ const allBlogs = [];
108
+ const getDatePublished = (entry) => {
109
+ const date =
110
+ entry?.draft?.blocks?.root?.props?.datePublished ||
111
+ entry?.blocks?.root?.props?.datePublished;
112
+ return date ? new Date(date).getTime() : 0;
113
+ };
114
+
115
+ for (let [key, value] of Object.entries(data)) {
116
+ const category =
117
+ value?.draft?.blocks?.root?.props?.category ||
118
+ value?.blocks?.root?.props?.category;
119
+ if (category === "blog") {
120
+ value = { ...value, parentFolder: value.folderUrl };
121
+ allBlogs.push(value);
122
+ }
123
+ if (value.hasOwnProperty("children")) {
124
+ for (let [childKey, childValue] of Object.entries(value["children"])) {
125
+ const childCategory =
126
+ childValue?.draft?.blocks?.root?.props?.category ||
127
+ childValue?.blocks?.root?.props?.category;
128
+
129
+ if (childCategory === "blog") {
130
+ childValue = { ...childValue, parentFolder: value.folderUrl };
131
+ allBlogs.push(childValue);
132
+ }
133
+ }
134
+ }
135
+ }
136
+
137
+ return allBlogs.sort((a, b) => getDatePublished(b) - getDatePublished(a));
138
+ };
139
+
60
140
  require("dotenv").config();
61
141
  // Load PAT from env instead of hardcoding (fallback to old const for now but warn)
62
142
  exports.onPreInit = async (_, pluginOptions) => {
@@ -392,29 +472,8 @@ exports.sourceNodes = async (
392
472
 
393
473
  const firebaseData = await fetchBlogData();
394
474
 
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;
475
+ const allBlogs = getAllBlogs(firebaseData);
411
476
 
412
- if (childCategory === "blog") {
413
- allBlogs.push(childValue);
414
- }
415
- }
416
- }
417
- }
418
477
  for (const blog of allBlogs) {
419
478
  const id = createNodeId(`Blog-${blog.id}`);
420
479
  const node = {
@@ -456,81 +515,15 @@ exports.createPages = async ({ actions, store }, pluginOptions) => {
456
515
  const firebaseData = await fetch(
457
516
  `https://attain-finance-cms-default-rtdb.firebaseio.com/cms/brands/${brands[brand]}/pages.json`
458
517
  );
518
+ const firebaseJson = await firebaseData.json();
459
519
 
460
- const blogData = graphql(`
461
- query {
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
- // });
520
+ const firebaseBlogs = getAllBlogs(firebaseJson);
521
+ const blogsPerFolder = 5;
532
522
 
533
- const firebaseJson = await firebaseData.json();
523
+ const blogsWithFolderNumbers = firebaseBlogs.map((blog, idx) => {
524
+ const folderNumber = Math.floor(idx / blogsPerFolder) + 1;
525
+ return { ...blog, folderNumber };
526
+ });
534
527
 
535
528
  async function processPage(page, parentPath = "") {
536
529
  // Check for folderUrl first — handle nested folders immediately
@@ -608,7 +601,62 @@ exports.createPages = async ({ actions, store }, pluginOptions) => {
608
601
  }
609
602
  }
610
603
 
604
+ // async function processBlogs(page, parentPath = "") {
605
+ // if (page.template === "visual-editor" && page.published && page.type === "blog") {
606
+ // const {
607
+ // blocks: {
608
+ // content,
609
+ // root: {
610
+ // props: { pageUrl, layout },
611
+ // },
612
+ // },
613
+ // id,
614
+ // folderNumber,
615
+ // parentFolder
616
+ // } = page;
617
+
618
+ // // Create slice for each block
619
+ // await Promise.all(
620
+ // content.map(async (b) => {
621
+ // const name = b.props.component.name;
622
+ // const sliceId = `block--${pageUrl}--${name}--${id}--${crypto.randomUUID()}`;
623
+
624
+ // createSlice({
625
+ // id: sliceId,
626
+ // component: path.resolve(
627
+ // siteRoot,
628
+ // "src/cms/components/sliceWrapper.tsx"
629
+ // ),
630
+ // context: {
631
+ // componentPath: `./src/cms/components${b.props.component.path}/index.tsx`,
632
+ // ...b.props,
633
+ // },
634
+ // });
635
+
636
+ // b["sliceId"] = sliceId;
637
+ // return b;
638
+ // })
639
+ // );
640
+
641
+ // // Generate page file
642
+ // const pageSource = await generateBlogPage(content, layout, parentPath);
643
+ // const folderPath = pageUrl;
644
+ // const outPath = path.join(siteRoot, "src/cms/pages", `${parentFolder}/${folderNumber}/${folderPath}.tsx`);
645
+
646
+ // await fse.outputFile(outPath, pageSource);
647
+
648
+ // await createPage({
649
+ // path: `${parentFolder}/${folderNumber}/${folderPath}`,
650
+ // component: outPath,
651
+ // context: { ...page },
652
+ // });
653
+ // }
654
+ // }
655
+
611
656
  for (const page of Object.values(firebaseJson)) {
612
657
  await processPage(page);
613
658
  }
659
+ // for (const blog of blogsWithFolderNumbers) {
660
+ // await processBlogs(blog, brand === "LendDirect" ? "resources" : "blogs");
661
+ // }
614
662
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gatsby-attainlabs-cms",
3
- "version": "1.0.36",
3
+ "version": "1.0.38",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",