2022-10-14 13:11:33 +00:00
|
|
|
import { nodeAtPos } from "$sb/lib/tree.ts";
|
|
|
|
import { editor, markdown, system } from "$sb/silverbullet-syscall/mod.ts";
|
|
|
|
import { events } from "$sb/plugos-syscall/mod.ts";
|
2022-07-30 11:39:40 +00:00
|
|
|
|
|
|
|
type UnfurlOption = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function unfurlCommand() {
|
2022-10-14 13:11:33 +00:00
|
|
|
const mdTree = await markdown.parseMarkdown(await editor.getText());
|
|
|
|
const nakedUrlNode = nodeAtPos(mdTree, await editor.getCursor());
|
|
|
|
const url = nakedUrlNode!.children![0].text!;
|
2022-07-30 11:39:40 +00:00
|
|
|
console.log("Got URL to unfurl", url);
|
2022-10-14 13:11:33 +00:00
|
|
|
const optionResponses = await events.dispatchEvent("unfurl:options", url);
|
|
|
|
const options: UnfurlOption[] = [];
|
|
|
|
for (const resp of optionResponses) {
|
2022-07-30 11:39:40 +00:00
|
|
|
options.push(...resp);
|
|
|
|
}
|
2022-10-14 13:11:33 +00:00
|
|
|
const selectedUnfurl: any = await editor.filterBox(
|
2022-07-30 11:39:40 +00:00
|
|
|
"Unfurl",
|
|
|
|
options,
|
2022-10-12 09:47:13 +00:00
|
|
|
"Select the unfurl strategy of your choice",
|
2022-07-30 11:39:40 +00:00
|
|
|
);
|
|
|
|
if (!selectedUnfurl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2022-10-14 13:11:33 +00:00
|
|
|
const replacement = await system.invokeFunction(
|
2022-07-30 11:39:40 +00:00
|
|
|
"server",
|
|
|
|
"unfurlExec",
|
|
|
|
selectedUnfurl.id,
|
2022-10-12 09:47:13 +00:00
|
|
|
url,
|
2022-07-30 11:39:40 +00:00
|
|
|
);
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.replaceRange(
|
|
|
|
nakedUrlNode?.from!,
|
|
|
|
nakedUrlNode?.to!,
|
|
|
|
replacement,
|
|
|
|
);
|
2022-07-30 11:39:40 +00:00
|
|
|
} catch (e: any) {
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.flashNotification(e.message, "error");
|
2022-07-30 11:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
export function titleUnfurlOptions(): UnfurlOption[] {
|
2022-07-30 11:39:40 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: "title-unfurl",
|
|
|
|
name: "Extract title",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run on the server because plugs will likely rely on fetch for this
|
|
|
|
export async function unfurlExec(id: string, url: string): Promise<string> {
|
2022-10-14 13:11:33 +00:00
|
|
|
const replacement = await events.dispatchEvent(`unfurl:${id}`, url);
|
2022-09-02 13:47:44 +00:00
|
|
|
if (replacement.length === 0) {
|
|
|
|
throw new Error("Unfurl failed");
|
|
|
|
} else {
|
|
|
|
return replacement[0];
|
|
|
|
}
|
2022-07-30 11:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const titleRegex = /<title[^>]*>\s*([^<]+)\s*<\/title\s*>/i;
|
|
|
|
|
|
|
|
export async function titleUnfurl(url: string): Promise<string> {
|
2022-10-14 13:11:33 +00:00
|
|
|
const response = await fetch(url);
|
2022-07-30 11:39:40 +00:00
|
|
|
if (response.status < 200 || response.status >= 300) {
|
|
|
|
console.error("Unfurl failed", await response.text());
|
|
|
|
throw new Error(`Failed to fetch: ${await response.statusText}`);
|
|
|
|
}
|
2022-10-14 13:11:33 +00:00
|
|
|
const body = await response.text();
|
|
|
|
const match = titleRegex.exec(body);
|
2022-07-30 11:39:40 +00:00
|
|
|
if (match) {
|
|
|
|
return `[${match[1]}](${url})`;
|
|
|
|
} else {
|
|
|
|
throw new Error("No title found");
|
|
|
|
}
|
|
|
|
}
|