2022-11-24 11:04:00 +00:00
|
|
|
import { events } from "$sb/plugos-syscall/mod.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import { editor, markdown } from "$sb/silverbullet-syscall/mod.ts";
|
2022-11-24 11:04:00 +00:00
|
|
|
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
|
2022-11-24 15:08:51 +00:00
|
|
|
import { PublishEvent } from "$sb/app_event.ts";
|
2022-11-24 11:04:00 +00:00
|
|
|
|
|
|
|
export async function publishCommand() {
|
|
|
|
await editor.save();
|
|
|
|
const text = await editor.getText();
|
|
|
|
const pageName = await editor.getCurrentPage();
|
|
|
|
const tree = await markdown.parseMarkdown(text);
|
2023-08-23 17:08:21 +00:00
|
|
|
let { $share } = await extractFrontmatter(tree);
|
2022-11-24 11:04:00 +00:00
|
|
|
if (!$share) {
|
2023-08-23 17:08:21 +00:00
|
|
|
// Nothing to do here
|
2022-11-24 11:04:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!Array.isArray($share)) {
|
2023-08-23 17:08:21 +00:00
|
|
|
$share = [$share];
|
2022-11-24 11:04:00 +00:00
|
|
|
}
|
2022-11-24 15:08:51 +00:00
|
|
|
await editor.flashNotification("Sharing...");
|
2022-11-24 11:04:00 +00:00
|
|
|
try {
|
2023-05-23 18:53:53 +00:00
|
|
|
await publish(pageName, $share);
|
2022-11-24 11:04:00 +00:00
|
|
|
await editor.flashNotification("Done!");
|
|
|
|
} catch (e: any) {
|
|
|
|
await editor.flashNotification(e.message, "error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
async function publish(pageName: string, uris: string[]) {
|
2022-11-24 11:04:00 +00:00
|
|
|
for (const uri of uris) {
|
|
|
|
const publisher = uri.split(":")[0];
|
|
|
|
const results = await events.dispatchEvent(
|
|
|
|
`share:${publisher}`,
|
|
|
|
{
|
|
|
|
uri: uri,
|
|
|
|
name: pageName,
|
|
|
|
} as PublishEvent,
|
|
|
|
);
|
|
|
|
if (results.length === 0) {
|
|
|
|
throw new Error(`Unsupported publisher: ${publisher} for URI: ${uri}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|