- Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathutils.ts
68 lines (51 loc) · 1.74 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
import{DecorationType,BlockMapType,MapImageUrl}from"./types";
exportconstclassNames=(...classes: Array<string|undefined|false>)=>
classes.filter(a=>!!a).join(" ");
exportconstgetTextContent=(text: DecorationType[])=>{
returntext.reduce((prev,current)=>prev+current[0],"");
};
constgroupBlockContent=(blockMap: BlockMapType): string[][]=>{
constoutput: string[][]=[];
letlastType: string|undefined=undefined;
letindex=-1;
Object.keys(blockMap).forEach(id=>{
blockMap[id].value?.content?.forEach(blockId=>{
constblockType=blockMap[blockId]?.value?.type;
if(blockType&&blockType!==lastType){
index++;
lastType=blockType;
output[index]=[];
}
output[index].push(blockId);
});
lastType=undefined;
});
returnoutput;
};
exportconstgetListNumber=(blockId: string,blockMap: BlockMapType)=>{
constgroups=groupBlockContent(blockMap);
constgroup=groups.find(g=>g.includes(blockId));
if(!group){
return;
}
returngroup.indexOf(blockId)+1;
};
exportconstdefaultMapImageUrl: MapImageUrl=(image="",block)=>{
consturl=newURL(
`https://www.notion.so${
image.startsWith("/image") ? image : `/image/${encodeURIComponent(image)}`
}`
);
if(block&&!image.includes("/images/page-cover/")){
consttable=
block.value.parent_table==="space" ? "block" : block.value.parent_table;
url.searchParams.set("table",table);
url.searchParams.set("id",block.value.id);
url.searchParams.set("cache","v2");
}
returnurl.toString();
};
exportconstdefaultMapPageUrl=(pageId: string="")=>{
pageId=pageId.replace(/-/g,"");
return`/${pageId}`;
};