This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
helpers.ts
95 lines (84 loc) · 2.07 KB
/
helpers.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
import {
getVideoService,
videoIdProcessors,
} from "gatsby-remark-embed-video/src/config";
import {
IEmbedVideoOptions,
IVideoId,
IVideoService,
} from "gatsby-remark-embed-video/src/interfaces";
import { URL as BrowserURL } from "url";
export function embedVideoHTML(
type: string,
id: string,
options: IEmbedVideoOptions,
): string {
try {
const videoId: IVideoId = readVideoId(type, id);
const videoService = getVideoService(videoId.service);
const url = createUrl(videoId.id, videoService, options);
return createIframe(url, videoService, options);
} catch (e) {
return `<p style="color: red">Error: ${e.message}</p>`;
}
}
function readVideoId(type: string, id: string): IVideoId {
let videoId;
for (const processor of videoIdProcessors) {
try {
videoId = processor(id);
} catch (e) {
videoId = {};
}
if (Object.keys(videoId).length !== 0) {
return videoId as IVideoId;
}
}
if (type === "video") {
throw new TypeError("Id could not be processed");
}
return {
id,
service: type.toLowerCase(),
};
}
function createUrl(
videoId: string,
videoService: IVideoService,
options: IEmbedVideoOptions,
): string {
const videoUrl = videoService.embedUrl(videoId);
let url = new URL(videoUrl);
if (videoService.urlProcessing) {
url = videoService.urlProcessing(videoId, url as BrowserURL, options);
}
return url.toString();
}
function createIframe(
url: string,
videoService: IVideoService,
options: IEmbedVideoOptions,
) {
let iframeNode = `
<div class="embedVideo-container">
<iframe
width="${options.width}"
height="${options.height}"
src="${url}"
class="embedVideo-iframe"
allowfullscreen
></iframe>
</div>`;
if (options.noIframeBorder) {
iframeNode += `
<style>
.embedVideo-iframe {
border: 0
}
</style>`;
}
if (videoService.additionalHTML) {
iframeNode += videoService.additionalHTML;
}
return iframeNode;
}