-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgatsby-node.ts
70 lines (63 loc) · 2.04 KB
/
gatsby-node.ts
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
import { config } from 'dotenv';
config();
import { CreatePagesArgs } from 'gatsby';
import path from 'node:path';
import { BlogEntry, BlogEntryMetaItem } from './bcms/types/ts';
import { EntryContentParsedItem } from '@thebcms/types';
import { Client } from '@thebcms/client';
const bcms = new Client(
process.env.BCMS_ORG_ID || '',
process.env.BCMS_INSTANCE_ID || '',
{
id: process.env.BCMS_API_KEY_ID || '',
secret: process.env.BCMS_API_KEY_SECRET || '',
},
{
injectSvg: true,
},
);
export const createPages = async ({
actions: { createPage },
}: CreatePagesArgs) => {
const blogsTemplate = path.resolve('./src/templates/blogs.tsx');
const blogTemplate = path.resolve('./src/templates/blog.tsx');
const blogEntries = (await bcms.entry.getAll('blog')) as BlogEntry[];
const blogs: Array<{
meta: BlogEntryMetaItem;
content: EntryContentParsedItem[];
}> = [];
for (let i = 0; i < blogEntries.length; i++) {
const blogEntry = blogEntries[i];
const meta = blogEntry.meta.en as BlogEntryMetaItem;
createPage({
path: `/blog/${meta.slug}`,
component: blogTemplate,
context: {
data: {
meta,
content: blogEntry.content.en as EntryContentParsedItem[],
otherBlogs: blogEntries
.filter((blog) => blog.meta.en?.slug !== meta.slug)
.map((blog) => {
return blog.meta.en as BlogEntryMetaItem;
}),
bcmsConfig: bcms.getConfig(),
},
},
});
blogs.push({
meta,
content: blogEntry.content.en as EntryContentParsedItem[],
});
}
createPage({
path: '/',
component: blogsTemplate,
context: {
items: blogs.map((blog) => {
return blog.meta;
}),
bcmsConfig: bcms.getConfig(),
},
});
};