-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.ts
42 lines (36 loc) · 1.33 KB
/
context.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
import type { Links, LinksContext } from "./types";
export type { LinksContext };
import type { PageMetadataMap } from "$lib/loadPageMetadataMap";
export const linksKey = Symbol("contentfulRichTextLinks");
const getAssetLinksMap = (links: Links, key: "block" | "hyperlink") =>
new Map(links.assets?.[key]?.flatMap((link) => (link ? [[link?.sys?.id, link]] : [])));
const getAssetEntriesMap = (links: Links, key: "block" | "hyperlink") =>
new Map(links.entries?.[key]?.flatMap((link) => (link ? [[link?.sys?.id, link]] : [])));
export const createLinksContext = (
links: Links,
pageMetadataMap: PageMetadataMap,
): LinksContext => {
return {
pageMetadataMap,
links: {
assets: {
block: links.assets?.block?.filter(Boolean),
hyperlink: links.assets?.hyperlink?.filter(Boolean),
},
entries: {
block: links.entries?.block?.filter(Boolean),
hyperlink: links.entries?.hyperlink?.filter(Boolean),
},
},
linksAssetsMaps: {
block: getAssetLinksMap(links, "block"),
hyperlink: getAssetLinksMap(links, "hyperlink"),
},
linksEntriesMaps: {
block: getAssetEntriesMap(links, "block"),
hyperlink: getAssetEntriesMap(links, "hyperlink"),
},
};
};
export const blurhashesKey = Symbol("blurhashes");
export const imageSizeTypeKey = Symbol("sizeType");