2023-08-28 15:12:15 +00:00
|
|
|
import { YAML } from "$sb/syscalls.ts";
|
2023-01-21 12:37:55 +00:00
|
|
|
import type { WidgetContent } from "$sb/app_event.ts";
|
|
|
|
|
|
|
|
type EmbedConfig = {
|
|
|
|
url: string;
|
|
|
|
height?: number;
|
|
|
|
width?: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
function extractYoutubeVideoId(url: string) {
|
|
|
|
let match = url.match(/youtube\.com\/watch\?v=([^&]+)/);
|
|
|
|
if (match) {
|
|
|
|
return match[1];
|
|
|
|
}
|
|
|
|
match = url.match(/youtu.be\/([^&]+)/);
|
|
|
|
if (match) {
|
|
|
|
return match[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
export async function embedWidget(
|
2023-01-21 12:37:55 +00:00
|
|
|
bodyText: string,
|
2023-05-23 18:53:53 +00:00
|
|
|
): Promise<WidgetContent> {
|
2023-01-21 12:37:55 +00:00
|
|
|
try {
|
2023-05-23 18:53:53 +00:00
|
|
|
const data: EmbedConfig = await YAML.parse(bodyText) as any;
|
2023-01-21 12:37:55 +00:00
|
|
|
let url = data.url;
|
|
|
|
const youtubeVideoId = extractYoutubeVideoId(url);
|
|
|
|
if (youtubeVideoId) {
|
|
|
|
url = `https://www.youtube.com/embed/${youtubeVideoId}`;
|
|
|
|
// Sensible video defaults
|
|
|
|
data.width = data.width || 560;
|
|
|
|
data.height = data.height || 315;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
url,
|
|
|
|
height: data.height,
|
|
|
|
width: data.width,
|
|
|
|
};
|
|
|
|
} catch (e: any) {
|
|
|
|
return {
|
|
|
|
html: `ERROR: Could not parse body as YAML: ${e.message}`,
|
|
|
|
script: "",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|