Fixes #258
This commit is contained in:
@@ -409,6 +409,10 @@ functions:
|
||||
events:
|
||||
- unfurl:title-unfurl
|
||||
|
||||
embedWidget:
|
||||
path: ./embed.ts:embedWidget
|
||||
codeWidget: embed
|
||||
|
||||
# Random stuff
|
||||
statsCommand:
|
||||
path: ./stats.ts:statsCommand
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import * as YAML from "yaml";
|
||||
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;
|
||||
}
|
||||
|
||||
export function embedWidget(
|
||||
bodyText: string,
|
||||
): WidgetContent {
|
||||
try {
|
||||
const data: EmbedConfig = YAML.parse(bodyText) as any;
|
||||
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: "",
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user