-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltjs-utils.ts
More file actions
48 lines (42 loc) · 1.5 KB
/
Copy pathbuiltjs-utils.ts
File metadata and controls
48 lines (42 loc) · 1.5 KB
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
/* eslint-disable @typescript-eslint/no-explicit-any */
import dynamic from "next/dynamic";
export async function getComponentMap(sections:any) {
return new Promise(async (resolve) => {
const map:any = {};
for (let i = 0; i < sections.length; i++) {
if(!sections[i].template){
continue;
}
const section = sections[i];
const template = section.template.doc;
const templateFileName = template.name.replace(/[A-Z]/g, function (match: string) {
return '-' + match.toLowerCase();
});
map["section" + i] = import(
`./../components/plugins/richjava_about-basic/templates/${template.category}/${templateFileName}.tsx`
);
}
resolve(map);
});
}
export function getComponents(sections: any): Promise<React.ComponentType[]> {
return new Promise((resolve) => {
getComponentMap(sections).then((map: any) => {
const comps: React.ComponentType[] = [];
for (const key of Object.keys(map)) {
const comp = dynamic(() => map[key], {
loading: () => null
});
comps.push(comp);
}
resolve(comps);
});
});
}
export const widthForImage = (source: any) =>
source?.width
export const heightForImage = (source: any) =>
source?.height
export const collectionSlug = (entry: any) =>
entry._type ? entry._type.replace(/[A-Z]/g, (letter:any) => `-${letter.toLowerCase()}`) : '';
export const entrySlug = (entry: any) => entry && entry.slug && entry.slug.current ? entry.slug.current : entry.slug;