forked from sillsdev/docu-notion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
96 lines (88 loc) · 3.33 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
import { getHashParamValueJsonFromUrl } from "@metapages/hash-query";
import { MetapageDefinitionV3 } from "@metapages/metapage";
import { IDocuNotionContext, IPlugin } from "../../src/plugins/pluginTypes";
import fetch from "node-fetch";
export const embedToIframe: IPlugin = {
name: "embed-to-iframe",
regexMarkdownModifications: [
// replace embeds with iframes
{
regex: /\[embed\]\((\S+)\)/,
getReplacement: async (
context: IDocuNotionContext,
match: RegExpExecArray
): Promise<string> => {
const urlString = match[1];
if (!urlString) {
return match[0];
}
const url = new URL(urlString);
let possibleMetapageDefinition: MetapageDefinitionV3 | undefined =
undefined;
let iframeHeight = 300;
if (url.hostname.includes("metapage")) {
// We can get more info
possibleMetapageDefinition = getHashParamValueJsonFromUrl(
urlString,
"definition"
);
if (!possibleMetapageDefinition) {
// We can get more info
const urlBLobToFetchJson = new URL(urlString);
if (!urlBLobToFetchJson.pathname.endsWith("metapage.json")) {
urlBLobToFetchJson.pathname =
urlBLobToFetchJson.pathname +
(urlBLobToFetchJson.pathname.endsWith("/") ? "" : "/") +
"metapage.json";
}
try {
const r = await fetch(urlBLobToFetchJson.href);
possibleMetapageDefinition =
(await r.json()) as MetapageDefinitionV3;
} catch (err) {
//ignored
}
}
if (possibleMetapageDefinition) {
// We can get better layout info
const gridlayout =
possibleMetapageDefinition?.meta?.layouts?.["react-grid-layout"];
if (gridlayout) {
const rowHeight: number = gridlayout?.props?.rowHeight || 100;
const margin: number = gridlayout?.props?.margin?.[1] || 20;
const maxHeightBlocks = gridlayout?.layout.reduce(
(acc: number, cur: { h: number; y: number }) => {
return Math.max(acc, cur.h + cur.y);
},
1
);
const headerHeight = 40; // Copied from metapage.io code, how to get this in here?
const height =
maxHeightBlocks * rowHeight +
(margin * maxHeightBlocks - 1) +
headerHeight;
iframeHeight = height;
}
}
// fudge
iframeHeight += 70; // for the header plus padding, currently not part of the compute
}
// Check for redirects
try {
const maybeRedirectResponse = await fetch(urlString, {
redirect: "manual",
});
if (maybeRedirectResponse.status === 302) {
const location = maybeRedirectResponse.headers.get("location");
if (location) {
url.href = location;
}
}
} catch (err) {
// ignored
}
return `\n<iframe scrolling="yes" allow="fullscreen *; camera *; speaker *;" style={{width:"100%",height:"${iframeHeight}px",overflow:"hidden"}} src="${url.href}"></iframe>\n`;
},
},
],
};