-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathutils.ts
84 lines (76 loc) · 2.12 KB
/
utils.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
71
72
73
74
75
76
77
78
79
80
81
82
83
import {
DecorationType,
ColumnType,
RowContentType,
BlockType,
RowType,
} from "./types";
export const idToUuid = (path: string): string =>
`${path.slice(0, 8)}-${path.slice(8, 12)}-${path.slice(12, 16)}-${path.slice(16, 20)}-${path.slice(20)}`;
export const parsePageId = (id: string) => {
if (id) {
const rawId = id.replace(/\-/g, "").slice(-32);
return idToUuid(rawId);
}
};
export const getNotionValue = (
val: DecorationType[],
type: ColumnType,
row: RowType
): RowContentType => {
switch (type) {
case "text":
return getTextContent(val);
case "person":
return (
val.filter((v) => v.length > 1).map((v) => v[1]![0][1] as string) || []
);
case "checkbox":
return val[0][0] === "Yes";
case "date":
try {
if (val[0][1]![0][0] === "d") return val[0]![1]![0]![1]!.start_date;
else return "";
} catch (e) {
return "";
}
case "title":
return getTextContent(val);
case "select":
case "email":
case "phone_number":
case "url":
return val[0][0];
case "multi_select":
return val[0][0].split(",") as string[];
case "number":
return Number(val[0][0]);
case "relation":
return val
.filter(([symbol]) => symbol === "‣")
.map(([_, relation]) => relation![0][1] as string);
case "file":
return val
.filter((v) => v.length > 1)
.map((v) => {
const rawUrl = v[1]![0][1] as string;
const url = new URL(
`https://www.notion.so${
rawUrl.startsWith("/image")
? rawUrl
: `/image/${encodeURIComponent(rawUrl)}`
}`
);
url.searchParams.set("table", "block");
url.searchParams.set("id", row.value.id);
url.searchParams.set("cache", "v2");
return { name: v[0] as string, url: url.toString(), rawUrl };
});
default:
console.log({ val, type });
return "Not supported";
}
};
const getTextContent = (text: DecorationType[]) => {
return text.reduce((prev, current) => prev + current[0], "");
};